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