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