Javascript 检查元素是否存在于 jQuery 中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4592493/
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
Check if element exists in jQuery
提问by Nick
How do I check if an element exists if the element is created by .append()
method?
$('elemId').length
doesn't work for me.
如果元素是通过.append()
方法创建的,如何检查元素是否存在?
$('elemId').length
对我不起作用。
回答by Sarfraz
$('elemId').length
doesn't work for me.
$('elemId').length
对我不起作用。
You need to put #
before element id:
您需要#
在元素 id 之前放置:
$('#elemId').length
---^
With vanilla JavaScript, you don't need the hash (#
) e.g. document.getElementById('id_here')
, however when using jQuery, you do need to put hash to target elements based on id
just like CSS.
使用 vanilla JavaScript,您不需要 hash ( #
) eg document.getElementById('id_here')
,但是当使用 jQuery 时,您确实需要id
像 CSS 一样将 hash 放在目标元素上。
回答by Tapan kumar
Try to check the length of the selector, if it returns you something then the element must exists else not.
尝试检查选择器的长度,如果它返回一些东西,那么该元素必须存在,否则不存在。
if( $('#selector').length ) // use this if you are using id to check
{
// it exists
}
if( $('.selector').length ) // use this if you are using class to check
{
// it exists
}
回答by Gaurav123
Try this:
尝试这个:
if ($("#mydiv").length > 0){
// do something here
}
The length property will return zero if element does not exists.
如果元素不存在,则长度属性将返回零。
回答by lfender6445
How do I check if an element exists
如何检查元素是否存在
if ($("#mydiv").length){ }
If it is 0
, it will evaluate to false
, anything more than that true
.
如果是0
,它将评估为false
,除此之外的任何值true
。
There is no need for a greater than, less than comparison.
没有必要进行大于、小于比较。
回答by Mehran Hatami
your elemId
as its name suggests, is an Id
attribute, these are all you can do to check if it exists:
你elemId
顾名思义,是一种Id
属性,这些都是可以做,以检查它是否存在:
Vanilla JavaScript:in case you have more advanced selectors:
Vanilla JavaScript:如果您有更高级的选择器:
//you can use it for more advanced selectors
if(document.querySelectorAll("#elemId").length){}
if(document.querySelector("#elemId")){}
//you can use it if your selector has only an Id attribute
if(document.getElementById("elemId")){}
jQuery:
jQuery:
if(jQuery("#elemId").length){}
回答by loopmode
You can also use array-like notation and check for the first element.
The first element of an empty array or collection is simply undefined
, so you get the "normal" javascript truthy/falsy behaviour:
您还可以使用类似数组的表示法并检查第一个元素。空数组或集合的第一个元素很简单undefined
,因此您将获得“正常”的 javascript 真/假行为:
var el = $('body')[0];
if (el) {
console.log('element found', el);
}
if (!el) {
console.log('no element found');
}
回答by Webomatik
You can use native JS to test for the existence of an object:
您可以使用原生 JS 来测试对象是否存在:
if (document.getElementById('elemId') instanceof Object){
// do something here
}
Don't forget, jQuery is nothing more than a sophisticated (and very useful) wrapper around native Javascript commands and properties
不要忘记,jQuery 只不过是一个围绕原生 Javascript 命令和属性的复杂(且非常有用)的包装器
回答by DesignForLife
If you have a class on your element, then you can try the following:
如果您的元素上有一个类,那么您可以尝试以下操作:
if( $('.exists_content').hasClass('exists_content') ){
//element available
}