Bha*_*kar 6 html javascript jquery
我想检查一个元素是否存在于整个页面中.有没有办法通过jQuery知道元素是否存在于页面中?
例如:
<html>
<body>
<p id="para1" class="para_class"></p>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,我必须检查DOM中是否存在<p>id para1.在任何情况下,如果在'class'属性的帮助下,我们可以知道元素是否存在,它也会有所帮助.
对于元素ID:
if($('#para1').length){
//element with id exists
}
Run Code Online (Sandbox Code Playgroud)
对于元素类:
if($('.para_class').length){
//element with class exists
}
Run Code Online (Sandbox Code Playgroud)