简体   繁体   English

jQuery .not选择器是否与.delegate一起使用?

[英]Does the jQuery .not selector work with .delegate?

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. 我正在尝试对id #filter里面的所有<a>元素进行Ajax调用,这些元素没有类.noAjax ,不知怎的,我无法让它工作。 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. 当我尝试使用aa.ajax (我当然在尝试之前添加)作为.delegate的选择器时, .delegate没有任何作用。 with the jquery above the ajax works, but it tries to load content even when i click a link with a.noAjax 使用jjery上面的ajax工作,但它尝试加载内容,即使我点击链接与a.noAjax

Use the :not() selector instead: 请改用: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;
});

Or you could delegate to the ul element instead, since it's using a unique ID, for improved selector performance: 或者您可以委托给ul元素,因为它使用唯一ID,以提高选择器性能:

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

This: 这个:

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

Should be on the a elements, like this: 应该是a元素,像这样:

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

Or, what you're really after: 或者,你真正追求的是:

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

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). 另一种方法是选择#filter中不是a.noAjax所有元素(包括非锚标签)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM