gpt4 book ai didi

javascript - 是否有 jQuery 函数 "shortcut"if/else 开关?

转载 作者:行者123 更新时间:2023-11-28 11:13:42 25 4
gpt4 key购买 nike

在 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);

工作演示位于http://jsfiddle.net/alnitak/n2CLZ/

关于javascript - 是否有 jQuery 函数 "shortcut"if/else 开关?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10318071/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com