Tuesday 15 June 2010

javascript - Event capturing jQuery -


I need to capture an event instead of giving it bubbles. This is what I need: < Pre> & lt; Body & gt; & Lt; Div & gt; & Lt; / Div & gt; & Lt; / Body & gt;

With this sample code, I have a click event surrounded by a divisor and body. I want the body function to be called for the first time. How can I go about this?

Use Event Capturing instead: -

  $ ( "Body"). Get (0) .addEventListener ("click", function () {}, true);   

Check the final logic for "addEventListener" by default This is incorrect and the event is in Bubbling mode. If set to true then will act as a capturing event.

For cross browser implementation.

  var bodyEle = $ ("body"). Get (0); If (bodyEle.addEventListener) {bodyEle.addEventListener ("click", function () {}, true); } And if (bodyEle.attachEvent) {document.attachEvent ("onclick", function () {var event = window.event;}); }   

Use pre-eventbulbling by IE8 and by default so I've attached the document to the document instead of the body, so you must use the event object to get the object object . You should be very difficult for IE.

No comments:

Post a Comment