问题
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