在 jQuery 代码中间编写 if/else 语句感觉不对:
if ($("#test").length) {
// do smth
}
else {
// do smth else
}
有没有类似的东西:
$("#test").test(function(){
// do smth
},
function(){
// do smth else
});
最佳答案
Is there something similar to:
现在就有了!
(function($) {
$.fn.test = function(a, b) {
(this.length ? a : b).apply(this);
return this;
};
})(jQuery);
关于javascript - 是否有 jQuery 函数 "shortcut"if/else 开关?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10318071/