代码之家  ›  专栏  ›  技术社区  ›  tobiasmay

jQuery.not选择器是否与.delegate一起工作?

  •  3
  • tobiasmay  · 技术社区  · 14 年前

    我正试着对所有人进行Ajax调用 <a> id中的元素 #filter 没有课 .noAjax 不知怎么的,我不能让它工作。有人能看看我的语法吗?

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

    当我试图使用 a a.ajax (我当然在尝试之前添加了)作为 .delegate 什么都不管用。使用上面的jquery,ajax可以工作,但它会尝试加载内容,即使我单击了 a.noAjax

    3 回复  |  直到 14 年前
        1
  •  10
  •   Andy E    14 年前

    使用 :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)", ...);
    
        2
  •  0
  •   Nick Craver    14 年前

    这:

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

    应该在 a 元素,如:

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

    或者,你是什么 真正地 之后:

    $("#portfolio-list").delegate("li a:not(.noAjax)", "click", function() 
    
        3
  •  0
  •   Richard Marskell - Drackir Sunil Tandon    14 年前

    尝试将第一部分更改为:

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

    另一种方法将选择 #filter 那不是 a.noAjax (包括非锚定标签)。