You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If we were to click our newly added item, nothing would happen. This is because of the directly bound event that we attached previously. Direct events are only attached to elements at the time we called the `.on()` method for our existing collection of `<a>`'s, that is only the `<a>`'s that were found when we call `$('#list a')`
53
+
If we were to click our newly added item, nothing would happen. This is because of the directly bound event that we attached previously. Direct events are only attached to elements at the time we called the `.on()` method for our existing collection of `<a>`"s, that is only the `<a>`"s that were found when we call `$()`
51
54
52
55
## Event Propagation
53
56
Understanding how events propagate is an important factor in being able to leverage Event Delegation. Any time an anchor tags is clicked, a *click* event is fired for the:
@@ -65,30 +68,40 @@ Anytime one of these links is clicked you can think of it as if you were clickin
65
68
Since we know how events bubble we can created a delegated event that listens for a specific event to happen on our element
66
69
```
67
70
// attach a delegated event
68
-
$('#list').on('click', 'a', function(event){
71
+
$("#list").on("click", "a", function(event) {
72
+
69
73
event.preventDefault();
74
+
70
75
console.log( $(this).text() );
76
+
71
77
});
72
78
```
73
-
Notice for the second parameter to the `.on()` method we are telling it which selector to listen for. Now when a *click* event is triggered on our `<ul>`, our delegated event will check to see if the triggering element matches our selector (`'a'`). If it does, our anonymous function will execute. We have now attached a single *click* event listener to our `<ul>` instead of an unknown number of directly bound events on our `<a>`'s.
79
+
Notice for the second parameter to the `.on()` method we are telling it which selector to listen for. Now when a *click* event is triggered on our `<ul>`, our delegated event will check to see if the triggering element matches our selector (`"a"`). If it does, our anonymous function will execute. We have now attached a single *click* event listener to our `<ul>` instead of an unknown number of directly bound events on our `<a>`"s.
74
80
75
81
Now lets say that whenever a link is clicked we want to check and see if the `href` attribute starts with "http" and if it does we want to set the `target` attribute to `_blank`.
76
82
```
77
83
// attach a delegated event
78
-
$('#list').on('click', 'a', function(event){
79
-
var $elem = $(this);
80
-
if( $elem.is('[href^=http]') ){
81
-
$elem.attr('target', '_blank');
84
+
$("#list").on( "click", "a", function(event) {
85
+
86
+
var $elem = $( this );
87
+
88
+
if( $elem.is("[href^=http]") ) {
89
+
90
+
$elem.attr( "target", "_blank" );
91
+
82
92
}
93
+
83
94
});
84
95
```
85
-
This simply passes the `.is()` method a selector to see if the element's `href` attributes starts with "http". Also we have removed the `event.preventDefault();` statement, this is because we want the default action to happen (which is to following the `href`)
96
+
This simply passes the `.is()` method a selector to see if the element"s `href` attributes starts with "http". Also we have removed the `event.preventDefault();` statement, this is because we want the default action to happen (which is to following the `href`)
86
97
87
98
We can actually take this a step further and make our code simpiler and more concise by allowing the selector argument to `.on()` do our logic for us.
88
99
```
89
100
// attach a delegated event with a more refined selector
Copy file name to clipboardExpand all lines: page/events/event-extensions.md
+49-15
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
---
2
-
title: jQuery Event Extensions
3
-
level: advanced
2
+
title: jQuery Event Extensions
3
+
level: beginner
4
4
---
5
5
jQuery offers several ways to extend its event system to provide custom functionality when events are attached to elements. Internally in jQuery, these extensions are primarily used to ensure that standard events such as `submit` and `change` behave consistently across browsers. However, they can also be used to define new events with custom behavior.
6
6
@@ -58,23 +58,35 @@ Since fixHooks are an advanced feature and rarely used externally, we have not a
58
58
59
59
```
60
60
if ( jQuery.event.fixHooks.drop ) {
61
+
61
62
throw new Error("Someone else took the jQuery.event.fixHooks.drop hook!");
When these properties are defined, the following behavior occurs in the jQuery event system:
104
116
105
-
* Event handlers for the "pushy" event are actually attached to "click" -- both directly bound and delegated events.
106
-
* Special event hooks for "click" are called if they exist, *except* the `handle` hook for "pushy" is called when an event is delivered if one exists.
107
-
* Event handlers for "pushy" must be removed using the "pushy" event name, and are unaffected if "click" events are removed from the same elements.
108
-
117
+
* Event handlers for the "pushy" event are actually attached to "click" -- both directly bound and delegated events.
118
+
* Special event hooks for "click" are called if they exist, *except* the `handle` hook for "pushy" is called when an event is delivered if one exists.
119
+
* Event handlers for "pushy" must be removed using the "pushy" event name, and are unaffected if "click" events are removed from the same elements.
109
120
110
121
So given the special event above, this code shows that a pushy isn't removed by removing clicks. That might be an effective way to defend against an ill-behaved plugin that didn't namespace its removal of click events, for example:
111
122
112
123
```
113
124
var $p = $("p");
114
-
$p.on("click", function( e ) {
115
-
$("body").append("I am a " + e.type + "!"));
125
+
126
+
$p.on( "click", function( e ) {
127
+
128
+
$("body").append( "I am a " + e.type + "!" ));
129
+
116
130
});
117
-
$p.on("pushy", function( e ) {
118
-
$("body").append("I am pushy but still a " + e.type + "!");
131
+
132
+
$p.on( "pushy", function( e ) {
133
+
134
+
$("body").append( "I am pushy but still a " + e.type + "!" );
135
+
119
136
});
137
+
120
138
$p.trigger("click"); // triggers both handlers
139
+
121
140
$p.off("click");
141
+
122
142
$p.trigger("click"); // still triggers "pushy"
143
+
123
144
$p.off("pushy");
124
145
```
125
146
@@ -130,7 +151,7 @@ These two properties are often used in conjunction with a `handle` hook function
130
151
Many of the special event hook functions below are passed a `handleObj` object that provides more information about the event, how it was attached, and its current state. This object and its contents should be treated as read-only data, and only the properties below are documented for use by special event handlers. For the discussion below, assume an event is attached with this code:
0 commit comments