是否存在“存在” jQuery的功能? [英] Is there an "exists" function for jQuery?
本文介绍了是否存在“存在” jQuery的功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何检查jQuery中是否存在元素?
How can I check the existence of an element in jQuery?
我当前的代码是:
if ($(selector).length > 0) {
// Do something
}
有更优雅的方式来解决这个问题吗?也许是插件或函数?
Is there a more elegant way to approach this? Perhaps a plugin or a function?
推荐答案
在JavaScript中,一切都是真实的或虚假的,对于数字 0
(和NaN)表示 false
,其他一切 true
。所以你可以写:
In JavaScript, everything is 'truthy' or 'falsy', and for numbers 0
(and NaN) means false
, everything else true
. So you could write:
if ($(selector).length)
您不需要> 0
部分。
这篇关于是否存在“存在” jQuery的功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文