event options

This commit is contained in:
Ilya Kantor 2019-03-09 11:20:16 +03:00
parent c8e7222bb2
commit fa8ffbe998
3 changed files with 31 additions and 9 deletions

View file

@ -136,9 +136,9 @@ That is: for a click on `<td>` the event first goes through the ancestors chain
Handlers added using `on<event>`-property or using HTML attributes or using `addEventListener(event, handler)` don't know anything about capturing, they only run on the 2nd and 3rd phases.
To catch an event on the capturing phase, we need to set the 3rd argument of `addEventListener` to `true`.
To catch an event on the capturing phase, we need to set the event options of `addEventListener` to `{capture: true}` (or just `true`).
There are two possible values for that optional last argument:
There are two possible values `capture` option:
- If it's `false` (default), then the handler is set on the bubbling phase.
- If it's `true`, then the handler is set on the capturing phase.
@ -182,12 +182,16 @@ Please note that `P` shows up two times: at the end of capturing and at the star
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.
```smart header="To remove the handler, `removeEventListener` needs the same phase"
If we `addEventListener(..., true)`, then we should mention the same phase in `removeEventListener(..., true)` to correctly remove the handler.
```
## Summary
The event handling process:
- When an event happens -- the most nested element where it happens gets labeled as the "target element" (`event.target`).
- Then the event first moves from the document root down to the `event.target`, calling handlers assigned with `addEventListener(...., true)` on the way.
- Then the event first moves from the document root down to the `event.target`, calling handlers assigned with `addEventListener(...., true)` on the way (`true` is a shorthand for `{capture: true}`).
- Then the event moves from `event.target` up to the root, calling handlers assigned using `on<event>` and `addEventListener` without the 3rd argument or with the 3rd argument `false`.
Each handler can access `event` object properties: