Javascript 查找整个html页面中是否存在该元素

Javascript 查找整个html页面中是否存在该元素,javascript,jquery,html,Javascript,Jquery,Html,我想检查整个页面中是否存在元素。有没有办法通过jQuery知道页面中是否存在该元素 例如: <html> <body> <p id="para1" class="para_class"></p> </body> </html> 在上面的代码中,我必须检查id para1的是否存在于DOM中。在任何情况下,如果通过“class”属性的帮助,我们可以知道元素是否存在,这也会很有帮助。对

我想检查整个页面中是否存在元素。有没有办法通过jQuery知道页面中是否存在该元素

例如:

<html>
     <body>
        <p id="para1" class="para_class"></p>
     </body>
</html>

在上面的代码中,我必须检查id para1的
是否存在于DOM中。在任何情况下,如果通过“class”属性的帮助,我们可以知道元素是否存在,这也会很有帮助。

对于元素ID:

if($('#para1').length){
  //element with id exists
}
对于元素类:

if($('.para_class').length){
  //element with class exists
}

最近我遇到了同样的问题&这对我有好处

if ( $('#para1').lenght == 1 ){ // if the id exists its length will be 1 

      alert('This Id exists');

} elseif ( $('#para1').lenght == 0 ){ // if the id doesn't exists its length will be 0

      alert('This Id does not exists');
}