Article Directory
1. What is event delegation
- Delegate (bind) the event of the child element to the parent element
2. The benefits of event delegation
- Reduce the number of bindings
- Newly added elements can also enjoy previously delegated events
3. The principle of event delegation
- bubble
4. Who triggered the event
- child element
5. How to find the object that triggered the event
- event.target
- event.currentTarget
6. currentTarget VS target
- currentTarget requires that the element that binds the event must be the element that triggers the event
- target The element to which the event is bound is not necessarily the element that triggered the event
7. Examples
/* 可通过 id = value 传值,或 通过 data-key = value 传值 */
<view id="tapTest" data-hi="Weixin" bindtap="tapName"> Click me! </view>
Page({
tapName: function(event) {
console.log(event)
}
})
output