jQuery 使用jquery检查div是否存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6899175/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 21:31:14  来源:igfitidea点击:

Check if a div exists with jquery

jqueryhtmlexists

提问by Dementic

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');
    }
});

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

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

EDIT: 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?

编辑:令人遗憾的是,人们不想从三种不同的方法中学习什么是更好的方法。这个问题实际上不是关于“如何检查 div 是否存在”,而是关于哪种方法更好,如果有人可以解释,为什么更好?

采纳答案by karim79

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 neverbe null or falsy for that matter.

第一个是最简洁的,我会同意。前两个是相同的,但第一个短一点,因此您可以节省字节。第三个是完全错误的,因为该条件将始终评估为真,因为对象永远不会为空或假。

回答by Alex KeySmith

If you are simply checking for the existence of an ID, there is no need to go into jQuery, you could simply:

如果您只是检查 ID 是否存在,则无需进入jQuery,您可以简单地:

if(document.getElementById("yourid") !== null)
{
}

getElementByIdreturns nullif it can't be found.

getElementByIdnull如果找不到就返回。

Reference.

参考

If however you plan to use the jQueryobject later i'd suggest:

但是,如果您打算稍后使用jQuery对象,我建议:

$(document).ready(function() {
    var $myDiv = $('#DivID');

    if ( $myDiv.length){
        //you can now reuse  $myDiv here, without having to select it again.
    }


});

A selector always returns a jQueryobject, so there shouldn't be a need to check against null(I'd be interested if there is an edge case where you need to check for null- but I don't think there is).

选择器总是返回一个jQuery对象,因此不需要检查null(如果有需要检查的边缘情况,我会感兴趣null- 但我认为没有)。

If the selector doesn't find anything then length === 0which is "falsy" (when converted to bool its false). So if it finds something then it should be "truthy" - so you don't need to check for > 0. Just for it's "truthyness"

如果选择器没有找到任何东西,那么length === 0哪个是“假的”(当转换为 bool 时它的假)。所以如果它找到了一些东西,那么它应该是“真实的”——所以你不需要检查 > 0。只是因为它是“真实的”

回答by tskuzzy

As karim79 mentioned, the first is the most concise. However I could argue that the second is more understandable as it is not obvious/known to some Javascript/jQuery programmers that non-zero/false values are evaluated to truein if-statements. And because of that, the third method is incorrect.

正如 karim79 提到的,第一个是最简洁的。但是我可以争辩说,第二个更容易理解,因为对于一些 Javascript/jQuery 程序员来说,true在 if 语句中评估非零/假值并不明显/不知道。正因为如此,第三种方法是不正确的。