扩展:https://stackoverflow.com/a/31047/235633jQuery的“存在”功能与回调
有没有一种方法,我可以扩展的自定义jQuery函数使用回调?基本上我希望能够有一个选择器列表并检测它们的存在,如果它们存在,我需要显示它们。
而不是写一千if语句,我宁愿写一个函数,使用回调和'this'。
的家伙写道:
jQuery.fn.exists = function(){return this.length>0;}
if ($(selector).exists()) {
// Do something
}
但我需要:
$('.selector1, .selector2, .selector3').exists(function({
$(this).show();
});
只是使用一个if语句围绕整个事情。 – 2011-12-22 16:52:16
如果所有三个都存在,您是否只想显示它们,否则不显示它们中的任何一个? – 2011-12-22 16:55:52