[英]How do I find out whether an element with that ID exists or not?
I do a: 我做:
console.log($('#test'));
I know that test doesn't exist. 我知道测试不存在。 If I do a console.log, it doesn't output undefined/null. 如果我执行console.log,则不会输出undefined / null。 Rather it ouputs something like an empty array and when I check that array it looks like it returns the jQuery object itself. 而是输出类似空数组的内容,当我检查该数组时,它看起来像返回了jQuery对象本身。
I also tried: 我也尝试过:
if ($('#test')){
//do something
}
But it still doesn't work. 但这仍然行不通。 I want to know whether the ID I am selecting exists on page or not. 我想知道我选择的ID在页面上是否存在。 How do I do that using jQuery? 我该如何使用jQuery?
It's something like 20x faster to do this: 这样做的速度大约快20倍:
if (document.getElementById("test"))
compared to the jQuery operation to just determine if a DOM object with that id exists in the page. 与jQuery操作进行比较,以确定页面中是否存在具有该ID的DOM对象。 jQuery can do a lot for you, but when its general selector engine and general object structure isn't needed, it's not the quickest way to do things. jQuery可以为您做很多事情,但是当不需要通用选择器引擎和通用对象结构时,它并不是最快的处理方法。
As others have said, $("#test")
is always a valid jQuery object, even if #test
doesn't exist. 正如其他人所说, $("#test")
始终是有效的jQuery对象,即使#test
不存在也是如此。 If the #test
object doesn't exist, then $("#test")
will be a jQuery object that has no DOM objects in it (the internal array will have a .length === 0
), but it's still a valid object. 如果#test
对象不存在,则$("#test")
将是其中没有DOM对象的jQuery对象(内部数组将具有.length === 0
),但它仍然是有效的宾语。
Use '(' and ')' for 'if' statements, and check if the returned array has length greater than 0: 对'if'语句使用'('和')',并检查返回的数组的长度是否大于0:
if ($('#test').length > 0){
//do something
}
best way for this is to check length of the selected element 最好的方法是检查所选元素的长度
if ($('#test').length > 0){
//do something
}
But if you want to create a exist function jQuery welcomes you just add the line in your script 但是,如果您想创建一个存在的函数,jQuery欢迎您,只需在脚本中添加一行
jQuery.fn.exists = function(){return this.length>0;}
and now you can Check if element exist or not 现在您可以检查元素是否存在
if ($(selector).exists()) {
// Do something
}
In JavaScript, objects are always truthy , so using it in that fashion will always pass the condition. 在JavaScript中,对象始终是真实的 ,因此以这种方式使用对象将始终通过条件。
You need to check the length
property. 您需要检查length
属性。 A response of 0
is falsy , and will work as expected. 响应0
为falsy ,将按预期工作。
if ($('#test').length) {
// ...
}
This is unlike document.getElementById()
, which returns null
if the element with that id
attribute does not exist. 这不同于document.getElementById()
,如果具有该id
属性的元素不存在,则返回null
。
If this is confusing, you could always write a quick jQuery plugin. 如果这令人困惑,那么您总是可以编写一个快速的jQuery插件。
$.fn.exists = function() {
return !!this.length;
};
You can then call exists()
on a jQuery collection, to ensure that selector has matched at least one item. 然后,您可以在jQuery集合上调用exists()
,以确保选择器已匹配至少一项。
Use 采用
if ($('#test').length > 0){
//do something
}
the length tells you how many items were selected if it is 0 no element has the id test. 长度告诉您选择的项目数(如果为0),则没有元素进行id测试。
console.log($('#test'));
This won't print the value because it represents the object found in the DOM with the id test. 这不会显示该值,因为它表示在id测试中在DOM中找到的对象。 If you want to get values, use $("#test").val(); 如果要获取值,请使用$(“#test”)。val();。 or $("#test").html(); 或$(“#test”)。html();
If you want to check existence, do the length test as suggested above. 如果要检查是否存在,请按照上面的建议进行长度测试。
Also, if you're testing for the existence of a generated element (something you added to the DOM), make sure you checkout .live (http://api.jquery.com/live/). 另外,如果要测试是否存在生成的元素(已添加到DOM中的元素),请确保签出.live(http://api.jquery.com/live/)。 This is need for all elements that are created after the page is loaded. 这是页面加载后创建的所有元素所必需的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.