jQuery 如何使用jQuery验证DOM中存在的元素?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1027104/
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 10:27:00  来源:igfitidea点击:

How to verify an element exists in the DOM using jQuery?

jquery

提问by Toran Billups

Typically in JavaScript I do something like the below to verify an element does exist:

通常在 JavaScript 中,我会执行以下操作来验证元素确实存在:

if (document.getElementById('lblUpdateStatus')) {
    $("#lblUpdateStatus").text("");
}

But, using jQuery - how can I do the same type of thing?

但是,使用 jQuery - 我怎样才能做同样的事情呢?

采纳答案by Tony L.

The get method returns the matched DOM elements:

get 方法返回匹配的 DOM 元素:

if($("#lblUpdateStatus").get(0)){
    $("#lblUpdateStatus").click(function () { ... });
}

but I am not sure if it is a fast method.

但我不确定这是否是一种快速的方法。

回答by Dan F

$returns an array of matching elements, so check the length property and you're good to go

$返回匹配元素的数组,因此检查 length 属性,您就可以开始了

if ($('#lblUpdateStatus').length) {
    $("#lblUpdateStatus").text("");
}

回答by bang

The thing is that the jQuery will only do the requested action if the element exists :-), so you only need:

问题是 jQuery 只会在元素存在时执行请求的操作 :-),所以你只需要:

$("#lblUpdateStatus").text("");

回答by Tomas Aschan

I see no reason to use jQuery just for the sake of it. the $('#lblUpdateStatus')will basically go straight to document.getElementById('lblUpdateStatus'), as the selector has an anchor in it, so you're not really gaining anything. Also, for just checking if the DOM object exists, wrapping it in a jQuery object will create quite a lot of overhead.

我认为没有理由仅仅为了它而使用 jQuery。在$('#lblUpdateStatus')将基本直奔document.getElementById('lblUpdateStatus'),为选择中有一个锚,所以你没有真正得到什么。此外,为了检查 DOM 对象是否存在,将其包装在 jQuery 对象中会产生相当多的开销。

On the other hand, if just changing the text property of the object is what you want to do, you don't need to check for its existence if you use jQuery.

另一方面,如果您只想更改对象的 text 属性,那么如果您使用 jQuery,则不需要检查它是否存在。

if (document.getElementById('lblUpdateStatus')) {
    $("#lblUpdateStatus").text("");
}

will do the exact same thing as having just

将做与刚刚相同的事情

$("#lblUpdateStatus").text("");

回答by Ian Mackinnon

From jQuery 1.4 onwards you can use $.contains().

从 jQuery 1.4 开始,您可以使用$.contains().

var test = $("<div>");
$.contains(window.document, test[0]);  // false

$("body").append(test);
$.contains(window.document, test[0]);  // true

回答by stoimen

I wrote a post about that on my blog

我在我的博客上写了一篇关于这个的帖子

if ( $('#element_id').length > 0 )
   console.log('the element with element_id exists in the DOM');

回答by vitalets

If HTML string can be passed into $(), you can use parent.length:

如果可以将 HTML 字符串传入$(),则可以使用parent.length

$('<div>').length // 1, but it not exist on page

but

$('<div>').parent().length // 0