Thursday, 15 August 2013

javascript - Delegated events don't work in combination with :not() selector -


I want to do something on all clicks except a certain element

I have made a very simple example which shows this issue:.

My code:

  $ ('body'). 'Click', ': no ​​(a)', function () {// do stuff});  

I hope every click on & lt; A & gt; will not be taken into account, but this is not the case.

Am I doing something wrong or is this a bug in favor of jQuery?

There's a lot going on in that code, it's not natural Most importantly, the click event is actually linked to the body element, since this element is not an anchor, so you will always receive a warning. (The event delegation works because click event bubbles from a through all of their ancestors, which includes body This does not reach the document. .)

Check what you want to do. He will tell you the element which was actually clicked, but the actual click event is still bound to the body element:

 < Code> $ (': no ​​(a)')) (click on the alert ('one click has been found), function (e) {// E = event item if ($ (e.target)');} });  


No comments:

Post a Comment