2010-11-18 59 views
3

我試圖對所有<a>元素內的id #filter進行Ajax調用,這些元素沒有類.noAjax,並且不知何故我無法使其工作。有人可以看看我的語法嗎?jQuery .not選擇器是否適用於.delegate?

$("#filter").not($("a.noAjax")).delegate("ul#portfolio-list li a", "click", function() { 
    if ($.browser.msie) { 
     location.hash = "#/" + this.pathname; 
    } else { 
     location.hash = this.pathname; 
    } 
    return false; 
}); 

當我嘗試只是使用aa.ajax作爲選擇的.delegate什麼(我想之前的過程中加入),在所有工作。與Ajax的工作原理上面jQuery的,但它會嘗試甚至當我點擊與a.noAjax

回答

10

使用:not()選擇,而不是鏈接到加載內容:

$("#filter").delegate("ul#portfolio-list li a:not(.noAjax)","click",function() { 
    if ($.browser.msie) { 
     location.hash = "#/" + this.pathname; 
    } else { 
     location.hash = this.pathname; 
    } 
    return false; 
}); 

或者你也可以委託給ul元素代替,因爲它使用一個唯一的ID,以提高性能的選擇:

$("#portfolio-list").delegate("li a:not(.noAjax)", ...); 
+0

感謝您的所有答案。但使用.not和:之間的不同之處是什麼? – tobiasmay 2010-11-18 15:28:19

+1

@tobias:'.not'是在已獲取的元素集上運行的過濾方法。 ':not'是在查詢元素期間運行的選擇器。 '委託()'不會運行選擇器,直到點擊事件,這就是爲什麼你需要選擇器而不是方法。 – 2010-11-18 15:32:13

+0

感謝澄清 - 只是讓我的手在jquery上。 – tobiasmay 2010-11-18 15:46:37

0

此:

$("#filter").not($("a.noAjax")). 

應該對a元素,像這樣:

$("#filter a").not("a.noAjax") 

或者,你在做什麼真正後:

$("#portfolio-list").delegate("li a:not(.noAjax)", "click", function() 
0

嘗試改變第一部分是:

$("#filter a").not($(".noAjax")).del... 

另一種方式將選擇#filter中的所有元素,不是a.noAjax(包括非錨定標籤)。