扩展:https ://stackoverflow.com/a/31047/235633
有没有办法可以扩展这个自定义 jQuery 函数以使用回调?本质上,我希望能够拥有一个选择器列表并检测它们的存在,如果它们存在,我需要显示它们。
与其编写一千个 if 语句,我更愿意编写一个使用回调和“this”的函数。
那家伙写道:
jQuery.fn.exists = function(){return this.length>0;}
if ($(selector).exists()) {
// Do something
}
但是我需要:
$('.selector1, .selector2, .selector3').exists(function({
$(this).show();
});