How can I check if an element is within another one in jQuery?

后端 未结 2 1174
余生分开走
余生分开走 2021-01-31 16:07

Is there any direct way in JavaScript or jQuery to check if an element is within another one.

I\'m not referring to the $(this).parent as the element I wish

2条回答
  •  不知归路
    2021-01-31 16:52

    With jQuery >=1.4 (2010) you can use the very fast function jQuery.contains()

    This static method works with DOM elements, not with jQuery elements and returns true or false.

    jQuery.contains( container, descendant )
    

    Example: To check if a element is in the document you could do this:

    jQuery.contains( document.body, myElement )
    

提交回复
热议问题