测试空的jQuery选择结果 [英] Test for empty jQuery selection result

查看:114
本文介绍了测试空的jQuery选择结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我做

var s = $('#something');

接下来我想测试jQuery是否找到#something,即我想测试是否 s 为空。

and next I want to test if jQuery found #something, i.e. I want to test if s is empty.

我可以使用我可靠的 isempty()在它上面:

I could use my trusty isempty() on it:

function isempty(o) {
    for ( var i in o )
        return false;
    return true;
}

或者由于jQuery对象是数组,我想我可以测试 s.length

Or since jQuery objects are arrays, I suppose I could test s.length.

但它们似乎都不是jQuery的成语,不是非常jQueryesque。你有什么建议?

But neither seem quite in the idiom of jQuery, not very jQueryesque. What do you suggest?

推荐答案

使用s.length属性。

Use the s.length property.

if(s.length == 0) {
    ...
}

[edit] size()在jquery中弃用1.8
http://api.jquery.com/size/

[edit] size() deprecated in jquery 1.8 http://api.jquery.com/size/

这篇关于测试空的jQuery选择结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆