查找元素是否存在于整个html页面中 [英] Finding whether the element exists in whole html page

查看:111
本文介绍了查找元素是否存在于整个html页面中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查一个元素是否存在于整个页面中。有什么方法可以通过jQuery知道元素是否存在于页面中?

I want to check whether an element exists in the whole page or not. Is there any way to know if the element exists in the page through jQuery?

例如:

For example:

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

在上面的代码中,我必须检查< p> id para1 存在于DOM中或不存在。在任何情况下,如果在'class'属性的帮助下,我们可以知道元素是否存在,这也是有帮助的。

In the above code, I have to check whether <p> of id para1 exists in the DOM or not. In any case if with the help of the 'class' attribute we can know if the element exists, it also would be helpful.

推荐答案

<对于元素ID:

if($('#para1').length){
  //element with id exists
}

对于元素类:

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

这篇关于查找元素是否存在于整个html页面中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆