Testing objects for ancestor/descendent relationship in JavaScript or Jquery

后端 未结 3 1445
-上瘾入骨i
-上瘾入骨i 2020-12-09 17:21

I\'m trying to come up with a reusable JS or jQuery function that would allow me to test if one object is a DOM descendant of another.

I\'ve seen a model of testing

3条回答
  •  无人及你
    2020-12-09 17:51

    a.contains(b)
    

    This is a pure JavaScript solution using Node.contains. The function is inclusive, a.contains(a) evaluates to true.

    There's an edge case in IE9: if b is a text node, contains will always return false.

提交回复
热议问题