开发者

jQuery length > 0

I desided to make 开发者_StackOverflow社区my code clearer by replacing

if (wrappedSet.length > 0)

to something like

if (wrappedSet.exists())

Is any native jq function for this? Or the only way is to extend it using $.fn.exists = ... ?


You could simply use

if (wrappedSet.length) 

That will evaluate to TRUE if you have 1 or more elements and to FALSE if you have 0 elements


The function size does exactly this. On the other hand, why do you want to do this? The extra function call would reduce performance (albeit by an infinitesimally small amount) with no particular gain.

You could just do this:

if (wrappedSet.length) {

since 0 evaluates to false and any other number evaluates to true.


You're not the first one ;-)

http://forum.jquery.com/topic/selector-exists-would-be-a-nice-addition

And $.fn.exists = looks like the way to do it.


there's no native jquery function for it, you'll have to do an exists function like you said.

here's a link to a discussion in jquery about it:

http://forum.jquery.com/topic/selector-exists-would-be-a-nice-addition

its best to just use selector.length like the otehrs said. saves you the extra () :P

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜