Skip to content

Commit 4e1e753

Browse files
bobholtajpiano
authored andcommitted
add core styles to events section examples
1 parent b7985e6 commit 4e1e753

12 files changed

+716
-381
lines changed

page/events/event-delegation.md

+25-12
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,21 @@ We need to attach the same event handler to multiple elements. In this example
3636
We can attach a direct bind click event to each `<li>` using the `.on()` method, that will alert the text inside of it by doing the following:
3737
```
3838
// attach a directly bound event
39-
$('#list a').on('click', function(event) {
39+
$("#list a").on( "click", function(event) {
40+
4041
event.preventDefault();
42+
4143
console.log( $(this).text() );
44+
4245
});
4346
```
4447

4548
While this works perfectly fine, there are drawbacks. Consider this:
4649
```
4750
// add a new element on to our existing list
48-
$('#list').append('<li><a href="http://newsite.com">Item #101</a></li>');
51+
$("#list").append("<li><a href="http://newsite.com">Item #101</a></li>");
4952
```
50-
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 `$()`
5154

5255
## Event Propagation
5356
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
6568
Since we know how events bubble we can created a delegated event that listens for a specific event to happen on our element
6669
```
6770
// attach a delegated event
68-
$('#list').on('click', 'a', function(event){
71+
$("#list").on("click", "a", function(event) {
72+
6973
event.preventDefault();
74+
7075
console.log( $(this).text() );
76+
7177
});
7278
```
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.
7480

7581
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`.
7682
```
7783
// 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+
8292
}
93+
8394
});
8495
```
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`)
8697

8798
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.
8899
```
89100
// attach a delegated event with a more refined selector
90-
$('#list').on('click', 'a[href^=http]', function(event){
91-
$(this).attr('target', '_blank');
101+
$("#list").on( "click", "a[href^=http]", function(event) {
102+
103+
$( this ).attr( "target", "_blank" );
104+
92105
});
93106
```
94107

page/events/event-exercises.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title : Exercises
2+
title: Exercises
33
level: beginner
44
source: http://jqfundamentals.com/legacy
55
attribution:

page/events/event-extensions.md

+49-15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
2-
title : jQuery Event Extensions
3-
level: advanced
2+
title: jQuery Event Extensions
3+
level: beginner
44
---
55
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.
66

@@ -58,23 +58,35 @@ Since fixHooks are an advanced feature and rarely used externally, we have not a
5858

5959
```
6060
if ( jQuery.event.fixHooks.drop ) {
61+
6162
throw new Error("Someone else took the jQuery.event.fixHooks.drop hook!");
63+
6264
}
65+
6366
jQuery.event.fixHooks.drop = { props: [ "dataTransfer" ] };
6467
```
6568

6669
When there are known cases of different plugins wanting to attach to the drop hook, this solution might be more appropriate:
6770

6871
```
6972
var existingHook = jQuery.event.fixHooks.drop;
73+
7074
if ( !existingHook ) {
75+
7176
jQuery.event.fixHooks.drop = { props: [ "dataTransfer" ] };
77+
7278
} else {
79+
7380
if ( existingHook.props ) {
81+
7482
existingHook.props.push( "dataTransfer" );
83+
7584
} else {
85+
7686
existingHook.props = [ "dataTransfer" ];
87+
7788
}
89+
7890
}
7991
```
8092

@@ -102,24 +114,33 @@ jQuery.event.special.pushy = {
102114

103115
When these properties are defined, the following behavior occurs in the jQuery event system:
104116

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.
109120

110121
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:
111122

112123
```
113124
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+
116130
});
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+
119136
});
137+
120138
$p.trigger("click"); // triggers both handlers
139+
121140
$p.off("click");
141+
122142
$p.trigger("click"); // still triggers "pushy"
143+
123144
$p.off("pushy");
124145
```
125146

@@ -130,7 +151,7 @@ These two properties are often used in conjunction with a `handle` hook function
130151
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:
131152

132153
```
133-
$(".dialog").on("click.myPlugin", "button", {mydata: 42}, myHandler);
154+
$(".dialog").on( "click.myPlugin", "button", {mydata: 42}, myHandler );
134155
```
135156

136157
type: String
@@ -199,23 +220,36 @@ jQuery.event.special.multiclick = {
199220
delegateType: "click",
200221
bindType: "click",
201222
handle: function( event ) {
202-
var handleObj = event.handleObj,
203-
targetData = jQuery.data( event.target ),
204-
ret;
223+
224+
var handleObj = event.handleObj;
225+
226+
var targetData = jQuery.data( event.target );
227+
228+
var ret = null;
205229
206230
// If a multiple of the click count, run the handler
207231
targetData.clicks = ( targetData.clicks || 0 ) + 1;
232+
208233
if ( targetData.clicks % event.data === 0 ) {
234+
209235
event.type = handleObj.origType;
236+
210237
ret = handleObj.handler.apply( this, arguments );
238+
211239
event.type = handleObj.type;
240+
212241
return ret;
242+
213243
}
244+
214245
}
246+
215247
};
216248
217249
// Sample usage
218-
$("p").on("multiclick", { clicks: 3 }, function(e){
250+
$("p").on( "multiclick", {clicks: 3}, function(e) {
251+
219252
alert("clicked 3 times");
253+
220254
});
221255
```

page/events/event-helpers.md

+20-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title : Event Helpers
2+
title: Event Helpers
33
level: beginner
44
source: http://jqfundamentals.com/legacy
55
attribution:
@@ -20,8 +20,10 @@ Prior to jQuery 1.4, the `$.fn.hover` method required two functions.
2020

2121
```
2222
// The hover helper function
23-
$('#menu li').hover(function() {
24-
$(this).toggleClass('hover');
23+
$("#menu li").hover(function() {
24+
25+
$( this ).toggleClass("hover");
26+
2527
});
2628
```
2729

@@ -35,12 +37,19 @@ providing a long list of functions can be difficult to debug.
3537

3638
```
3739
// The toggle helper function
38-
$('p.expander').toggle(
39-
function() {
40-
$(this).prev().addClass('open');
41-
},
42-
function() {
43-
$(this).prev().removeClass('open');
44-
}
45-
);
40+
$("p.expander").toggle(
41+
42+
function() {
43+
44+
$( this ).prev().addClass("open");
45+
46+
},
47+
48+
function() {
49+
50+
$( this ).prev().removeClass("open");
51+
52+
}
53+
54+
);
4655
```

0 commit comments

Comments
 (0)