Global jQuery `.click ()`

I would like to fire an event when something on the page is clicked and then process normally. For example, a click will be triggered, I will see if the target matches any, warned if this happened, and then continue the click event (no preventDefault() ).

+6
javascript jquery click
source share
3 answers
 $(document).click(function(e) { // e.target is the element which has been clicked. }); 

This will handle all click events unless the handler prevents the bubble event (by calling the stopPropagation () method of the event object).

+14
source share
 $("body").click(function (event) { // Your stuff here } 
+1
source share

3 options for you:

This is how .live () works in jquery. All the bubbles are at the top, and it matches the selector of your choice. http://api.jquery.com/live/

A more efficient way to do this is to use .delegate or provide context for .live (), so you don't have to bubble to the top. http://api.jquery.com/delegate/

If you want to do this manually, attach a 'click' to the document and use .closest () to find the closest matching selector: http://api.jquery.com/closest/

This is still a concept, a delegation event, as already mentioned.

0
source share

All Articles