我想检查一个元素是否存在于整个页面中。有什么办法可以通过jQuery知道元素是否存在于页面中?
例如:
<html>
<body>
<p id="para1" class="para_class"></p>
</body>
</html>
在上面的代码中,我必须检查是否<p>
id para1 是否存在于 DOM 中。在任何情况下,如果借助“class”属性我们可以知道该元素是否存在,这也会有所帮助。
最佳答案
对于元素 ID:
if($('#para1').length){
//element with id exists
}
对于元素类:
if($('.para_class').length){
//element with class exists
}
关于javascript - 查找元素是否存在于整个html页面中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7227087/