我正在寻找一种较短的写作方式:
$('div')
.filter(function(value) {
return runMyTestFunction(value);
})
.hide()
.end()
.filter(function(value) {
return !runMyTestFunction(value);
})
.show();
希望有以下几点:
$('div')
.filter(function(value) {
return runMyTestFunction(value);
})
.hide()
.end()
.remove(theLastWrappedSetPoppedOfftheJqueryStack)
.show();
我想将'runMyTestFunction'内联定义为lambda,因为我认为它会使代码更清晰但是我必须复制它。
答案 0 :(得分:9)
你可以这样做:
$('div')
.filter(runMyTestFunction);
.hide()
.end()
.not(runMyTestFunction)
.show();
如果您不想两次运行该方法:
$('div')
.hide() // hide all
.not(runMyTestFunction)
.show();
或者,如果您明确要隐藏某些元素:
var elements = $('div');
var toRemove = elements.filter(runMyTestFunction).hide();
elements.not(toRemove).show();