检查一个div是否存在与jquery [英] Check if a div exists with jquery

查看:193
本文介绍了检查一个div是否存在与jquery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能存在重复:

是否存在存在函数为jQuery

是的,我知道这已经被问了很多。
但是,它让我感到困惑,因为谷歌搜索的结果显示了不同的方法(列于下面)

Yes, I know this has been asked a lot. But, it confuses me, since the results on google for this search show different methods (listed below)

$(document).ready(function() {
    if ($('#DivID').length){
        alert('Found with Length');
    }

    if ($('#DivID').length > 0 ) {
        alert('Found with Length bigger then Zero');
    }

    if ($('#DivID') != null ) {
        alert('Found with Not Null');
    }
});

3中的哪一个是检查div是否存在的正确方法?

Which one of the 3 is the correct way to check if the div exists?

编辑:
看到人们不想从三种不同的方法中学习什么是更好的方法是一件很难的事。这个问题实际上并不是如何检查div是否存在,而是关于哪种方法更好,如果有人能解释,为什么它更好?

It's a pitty to see that people do not want to learn what is the better approach from the three different methods. This question is not actually on "How to check if a div exists" but it's about which method is better, and, if someone could explain, why it it better?

推荐答案

第一个是最简洁的,我会一起去。前两个是相同的,但第一个是短一点,所以你会节省字节。第三个是明显错误的,因为这个条件总是会评估真实的,因为对于这个问题,对象永远不会是空的或者虚假的。

The first is the most concise, I would go with that. The first two are the same, but the first is just that little bit shorter, so you'll save on bytes. The third is plain wrong, because that condition will always evaluate true because the object will never be null or falsy for that matter.

这篇关于检查一个div是否存在与jquery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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