Does the jQuery .not selector work with .delegate?

梦想的初衷 提交于 2019-12-21 19:53:05

问题


I'm trying to make an Ajax call on all <a> elements inside the id #filter which don't have the class .noAjax and somehow i can't get it to work. could someone have a look at my syntax please?

$("#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;
});

when i try to just use a or a.ajax (which i of course added before trying) as the selector for .delegate nothing works at all. with the jquery above the ajax works, but it tries to load content even when i click a link with a.noAjax


回答1:


Use the :not() selector instead:

$("#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;
});

Or you could delegate to the ul element instead, since it's using a unique ID, for improved selector performance:

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



回答2:


This:

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

Should be on the a elements, like this:

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

Or, what you're really after:

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



回答3:


Try changing the first part to this:

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

The other way will select all elements in #filter that are not a.noAjax (including non-anchor tags).



来源:https://stackoverflow.com/questions/4216306/does-the-jquery-not-selector-work-with-delegate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!