Suppose there is a div which contains a link ( a href) and there are three event listeners - on-click- 1) for the entire page, 2) on div 3) a tag. If the user clicks on the a tag, how are the listeners triggered? What is the order of them being registered?
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- Keeping track of variable instances
- Can php detect if javascript is on or not?
Essentially, it depends. There are 2 phases for events, Capturing (happens first), which goes document down, and Bubbling which goes element up.
JS can do both, which is why when creating a custom Event listened you have the third boolean variable, e.g.
parent.addEventListener('click',doSomething2,true) child.addEventListener('click',doSomething,false)
If its last argument is true the event handler is set for the capturing phase, if it is false the event handler is set for the bubbling phase.
Referring back to the sample code and to quote this page:
The page I linked above has way more information, but hopefully that answers the basic question.