WeChat applet (event delegation)


1. What is event delegation

  1. Delegate (bind) the event of the child element to the parent element

2. The benefits of event delegation

  1. Reduce the number of bindings
  2. Newly added elements can also enjoy previously delegated events

3. The principle of event delegation

  1. bubble

4. Who triggered the event

  1. child element

5. How to find the object that triggered the event

  1. event.target
  2. event.currentTarget

6. currentTarget VS target

  1. currentTarget requires that the element that binds the event must be the element that triggers the event
  2. 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
insert image description here

Guess you like

Origin blog.csdn.net/qq_45532769/article/details/128835908