测试空的jQuery选择结果


85

说我同意

var s = $('#something');

接下来,我要测试jQuery是否发现了#something,即我想测试jQuery是否s为空。

我可以isempty()对它使用信任:

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

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

但是,在jQuery的习语中似乎都没有,在jQueryesque中不是。你有什么建议?



墨客,是的,类似的问题,但我没有在.size()那儿看到答案,我很喜欢。


Answers:




-1

对我来说,更多的jQueryesque解决方案是:

jQuery.fn.isEmpty = function(fun){ if (this.length === 0) { fun(); } return this; };

这让我以典型的方式写:

$("#sel").fadeOut(500,afterFade).isEmpty(insteadOfFade);
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.