擴展: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