Skip to content

Commit c7da1a0

Browse files
committed
closes #3119
1 parent e82267e commit c7da1a0

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

2-ui/2-events/02-bubbling-and-capturing/article.md

+15-7
Original file line numberDiff line numberDiff line change
@@ -126,20 +126,21 @@ The standard [DOM Events](http://www.w3.org/TR/DOM-Level-3-Events/) describes 3
126126
2. Target phase -- the event reached the target element.
127127
3. Bubbling phase -- the event bubbles up from the element.
128128

129-
Here's the picture of a click on `<td>` inside a table, taken from the specification:
129+
Here's the picture, taken from the specification, of the capturing `(1)`, target `(2)` and bubbling `(3)` phases for a click event on a `<td>` inside a table:
130130

131131
![](eventflow.svg)
132132

133133
That is: for a click on `<td>` the event first goes through the ancestors chain down to the element (capturing phase), then it reaches the target and triggers there (target phase), and then it goes up (bubbling phase), calling handlers on its way.
134134

135-
**Before we only talked about bubbling, because the capturing phase is rarely used. Normally it is invisible to us.**
135+
Until now, we only talked about bubbling, because the capturing phase is rarely used.
136136

137-
Handlers added using `on<event>`-property or using HTML attributes or using two-argument `addEventListener(event, handler)` don't know anything about capturing, they only run on the 2nd and 3rd phases.
137+
In fact, the capturing phase was invisible for us, because handlers added using `on<event>`-property or using HTML attributes or using two-argument `addEventListener(event, handler)` don't know anything about capturing, they only run on the 2nd and 3rd phases.
138138

139139
To catch an event on the capturing phase, we need to set the handler `capture` option to `true`:
140140

141141
```js
142142
elem.addEventListener(..., {capture: true})
143+
143144
// or, just "true" is an alias to {capture: true}
144145
elem.addEventListener(..., true)
145146
```
@@ -180,9 +181,10 @@ The code sets click handlers on *every* element in the document to see which one
180181

181182
If you click on `<p>`, then the sequence is:
182183

183-
1. `HTML` -> `BODY` -> `FORM` -> `DIV` (capturing phase, the first listener):
184-
2. `P` (target phase, triggers two times, as we've set two listeners: capturing and bubbling)
185-
3. `DIV` -> `FORM` -> `BODY` -> `HTML` (bubbling phase, the second listener).
184+
1. `HTML` -> `BODY` -> `FORM` -> `DIV -> P` (capturing phase, the first listener):
185+
2. `P` -> `DIV` -> `FORM` -> `BODY` -> `HTML` (bubbling phase, the second listener).
186+
187+
Please note, the `P` shows up twice, because we've set two listeners: capturing and bubbling. The target triggers at the end of the first and at the beginning of the second phase.
186188

187189
There's a property `event.eventPhase` that tells us the number of the phase on which the event was caught. But it's rarely used, because we usually know it in the handler.
188190

@@ -199,6 +201,12 @@ elem.addEventListener("click", e => alert(2));
199201
```
200202
````
201203
204+
```smart header="The `event.stopPropagation()` during the capturing also prevents the bubbling"
205+
The `event.stopPropagation()` method and its sibling `event.stopImmediatePropagation()` can also be called on the capturing phase. Then not only the futher capturing is stopped, but the bubbling as well.
206+
207+
In other words, normally the event goes first down ("capturing") and then up ("bubbling"). But if `event.stopPropagation()` is called during the capturing phase, then the event travel stops, no bubbling will occur.
208+
```
209+
202210
203211
## Summary
204212
@@ -216,7 +224,7 @@ Each handler can access `event` object properties:
216224
217225
Any event handler can stop the event by calling `event.stopPropagation()`, but that's not recommended, because we can't really be sure we won't need it above, maybe for completely different things.
218226
219-
The capturing phase is used very rarely, usually we handle events on bubbling. And there's a logic behind that.
227+
The capturing phase is used very rarely, usually we handle events on bubbling. And there's a logical explanation for that.
220228
221229
In real world, when an accident happens, local authorities react first. They know best the area where it happened. Then higher-level authorities if needed.
222230

0 commit comments

Comments
 (0)