jQuery("a").not("div#mnuMain a").live("click", function(event){
event.preventDefault();
alert("yes I got u");
});
How to make it work?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Try putting it all in the main selector:
Example: http://jsfiddle.net/8Tkex/
EDIT:
The reason using
.not()didn’t work is that when you use jQuery’slive()method, you’re not actually placing the click handler on the element. Instead you’re placing it at the root of the document.This works because all click (and other) events on the page “bubble up” from the element that actually received the event, all the way up to the root, thus firing the handler that you placed at the root using
.live().Because this occurs for every click on the page, jQuery needs to know which item received the click so it can determine which (if any) handler to fire. It does this using the
selectoryou used when you called.live().So if you did:
…jQuery compares the
"a"selector to everyclickevent that is received.So when you do:
…then jQuery uses
"a:not(div#mnuMain a)"for the comparison.But if you do
…the selector ends up looking like
"a.not(div#mnuMain a)", which wouldn’t match anything, since there’s no.notclass on the<a>element.I think some methods may work with
live(), but.not()isn’t one of them.If you’re ever curious about what the selector looks like for your jQuery object, save your object to a variable, log it to the console and look inside. You’ll see the
selectorproperty that jQuery uses.…should output to the console something like:
This is the output I get from Safari’s console.