How do I check whether a jQuery element is in the DOM?

后端 未结 11 1894
我寻月下人不归
我寻月下人不归 2020-11-28 04:46

Let\'s say that I define an element

$foo = $(\'#foo\');

and then I call

$foo.remove()

from some event. My

11条回答
  •  一向
    一向 (楼主)
    2020-11-28 05:18

    I just realized an answer as I was typing my question: Call

    $foo.parent()
    

    If $f00 has been removed from the DOM, then $foo.parent().length === 0. Otherwise, its length will be at least 1.

    [Edit: This is not entirely correct, because a removed element can still have a parent; for instance, if you remove a

      , each of its child
    • s will still have a parent. Use SLaks' answer instead.

提交回复
热议问题