如何检查DOM中是否包含jQuery元素? [英] How do I check whether a jQuery element is in the DOM?
本文介绍了如何检查DOM中是否包含jQuery元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我定义了一个元素
$foo = $('#foo');
然后我打电话
$foo.remove()
从某个事件开始.我的问题是,如何检查$ foo是否已从DOM中删除?我发现$foo.is(':hidden')
可以工作,但是如果我仅调用$foo.hide()
,那当然也将返回true.
from some event. My question is, how do I check whether $foo has been removed from the DOM or not? I've found that $foo.is(':hidden')
works, but that would of course also return true if I merely called $foo.hide()
.
推荐答案
像这样:
if (!jQuery.contains(document, $foo[0])) {
//Element is detached
}
如果元素的父元素之一被删除(在这种情况下,元素本身仍将具有父元素),这仍然会起作用.
This will still work if one of the element's parents was removed (in which case the element itself will still have a parent).
这篇关于如何检查DOM中是否包含jQuery元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文