[微信小程序]微信小程序点击事件返回值的target分析

微信小程序点击事件返回值的target分析

测试过程

在微信小程序中创建以下图片

这里写图片描述

然后在调试中点击下面第5个。
console返回两个e
第一个e是第5块小块的e
这里写图片描述

第二个e是下面全部9小块组成的大块的e
这里写图片描述

可以看到,currentTarget节点是不一样的。

分析

在HTML或者WXML这些基于XML的树形结构的界面布局方式中,元素与元素之间是有层级关系的,子级元素上触发的事件,可以向父级元素逐层向上传递,所以,父级元素上也可以捕获子级元素上的事件并进行逻辑处理。
1. 使用 bind 开头的事件绑定,这种绑定不会阻止冒泡事件向上冒泡
2. 使用 catch 开头的事件绑定,这种绑定可以阻止冒泡事件向上冒泡

结论

event对象中
- target是事件产生的源头组件
- currentTarget则是当前捕获这个事件的组件。

(current - adj. 现在的; 最近的; 流行的; 流传的; n. 电流; 趋势; 水流; 涌流; )

target.id/currentTarget.id 为 目标事件的id
这里写图片描述

测试使用的代码

<view class="section">
    <movable-area style="height: 300px;width: 300px; background: red;">
        <movable-view x="{{x}}" y="{{y}}" direction="all" bindtouchmove="b1" out-of-bounds="true">1</movable-view>

        <movable-view x="{{x}}" y="{{y}}" direction="all" bindtouchmove="b1" out-of-bounds="true">2</movable-view>

        <movable-view x="{{x}}" y="{{y}}" direction="all" bindtouchmove="b1" out-of-bounds="true">3</movable-view>

        <movable-view x="{{x}}" y="{{y}}" direction="all" bindtouchmove="b1" out-of-bounds="true">4</movable-view>

        <movable-view x="{{x}}" y="{{y}}" direction="all" bindtouchmove="b1" out-of-bounds="true">5</movable-view>

        <movable-view x="{{x}}" y="{{y}}" direction="all" bindtouchmove="b1" out-of-bounds="true">6</movable-view>

        <movable-view x="{{x}}" y="{{y}}" direction="all" bindtouchmove="b1" out-of-bounds="true">7</movable-view>

        <movable-view x="{{x}}" y="{{y}}" direction="all" bindtouchmove="b1" out-of-bounds="true">8</movable-view>

        <movable-view x="{{x}}" y="{{y}}" direction="all" bindtouchmove="b1" out-of-bounds="true">9</movable-view>
    </movable-area>

<view style="height: 300px;width: 300px; background: red;" class="main" bindtap="viewmove">
    <view x="{{x}}" y="{{y}}" direction="all" bindtouchmove="viewmove" out-of-bounds="true" class="view">1</view>

    <view x="{{x}}" y="{{y}}" direction="all" bindtouchmove="viewmove" out-of-bounds="true" class="view">2</view>

    <view x="{{x}}" y="{{y}}" direction="all" bindtouchmove="viewmove" out-of-bounds="true" class="view">3</view>

    <view x="{{x}}" y="{{y}}" direction="all" bindtouchmove="viewmove" out-of-bounds="true" class="view">4</view>

    <view x="{{x}}" y="{{y}}" direction="all" bindtouchmove="viewmove" out-of-bounds="true" class="view" bindtap="viewmove">5</view>

    <view x="{{x}}" y="{{y}}" direction="all" bindtouchmove="viewmove" out-of-bounds="true" class="view">6</view>

    <view x="{{x}}" y="{{y}}" direction="all" bindtouchmove="viewmove" out-of-bounds="true" class="view">7</view>

    <view x="{{x}}" y="{{y}}" direction="all" bindtouchmove="viewmove" out-of-bounds="true" class="view">8</view>

    <view x="{{x}}" y="{{y}}" direction="all" bindtouchmove="viewmove" out-of-bounds="true" class="view">9</view>
</view>

    <view class="btn-area">
        <button size="mini" bindtap="tap">click me to move to (30px, 30px)</button>
    </view>

    。
    。
    。
    。
    。。

    。。

    。
</view>
.k{
  background: green;
  height: 100px;
  width:  100px;
  position:absolute;
}

movable-view{
  height: 98px;
  width: 98px;
  background: blue;
  position:relative;
  border:1px dashed #fff;
}


.view{
  height: 98px;
  width: 98px;
  background: blue;
  position:relative;
  border:1px dashed #fff;
  display: inline-block;
}
.main{
  margin-top:10px;
}
//index.js
//获取应用实例
var app = getApp()
Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
      x:0,
      y:0
  },
  onLoad: function () {
    console.log('onLoad')
    var that = this
    //调用应用实例的方法获取全局数据
    app.getUserInfo(function(userInfo){
      //更新数据
      that.setData({
        userInfo:userInfo
      })
    })
  },  tap: function(e) {
        this.setData({
            x: 30,
            y: 30
        });},
  scroll:function(){
    console.log("haha")
  },
    move:function(e){
      this.setData({
              left:e.touches[0].clientX-60,
              top:e.touches[0].clientY-60
          })
      console.log(e)
    },
    b1:function (e) {
        //console.log("e")
        console.log(e)
        //console.log(this.data.x)
    },
    viewmove:function(e){
        viewmove(e,this)
    }

})

function viewmove(e,that){
    console.log(e)
}
### 微信小程序中的冒泡事件参数传递机制 在微信小程序中,冒泡事件是指当某个组件上的事件被触发后,该事件会沿着 DOM 树向上逐级传播到其父节点。这种特性允许开发者在一个父容器上监听多个子组件的事件,从而简化代码逻辑并提高性能。 #### 冒泡事件的工作原理 冒泡事件的核心在于事件从目标组件出发,逐步向上传播至顶层父组件的过程。在这个过程中,如果某一层捕获到了事件,则可以通过 `e.detail` 接收来自子组件的数据[^3]。具体来说: - **事件对象结构**:微信小程序事件对象通常包含以下几个部分: - `type`: 事件类型(如 `tap`, `longtap` 等)。 - `timeStamp`: 时间戳。 - `target`: 当前事件的目标组件的信息。 - `currentTarget`: 绑定当前事件处理函数的组件信息。 - `detail`: 自定义数据字段,用于携带额外信息给父组件。 - **自定义数据传输**:通过设置 `data-*` 属性或者直接修改 WXML 中绑定事件的对象属性,可以将特定参数附加到事件对象的 `detail` 字段中[^4]。 --- #### 示例代码展示 以下是基于微信小程序实现冒泡事件参数传递的一个简单例子: ```html <!-- index.wxml --> <view bindtap="handleParentTap"> Parent View (点击这里不会触发任何操作) <button catchtap="handleChildTap" data-message="我是子按钮发送的消息">Click Me</button> </view> ``` ```javascript // index.js Page({ handleParentTap(e) { console.log('Parent Tap Event:', e); if (e.detail && e.detail.message) { console.log(`接收到子组件消息: ${e.detail.message}`); } else { console.log('未接收到来自子组件的消息'); } }, handleChildTap(e) { const message = e.currentTarget.dataset.message; this.triggerEvent('childMessage', { message }); // 向外抛出自定义事件 console.log('Child Button Clicked, Message Sent:', message); // 构造新的 detail 数据供上级使用 return { detail: { message: '这是由子组件构造的新消息' } }; } }); ``` 上述代码展示了以下几点功能: 1. 子组件 (`button`) 使用 `catchtap` 来阻止事件冒泡的同时主动调用 `triggerEvent()` 方法向外广播自定义事件。 2. 如果希望让事件继续冒泡,则可以在返回值中重新构建 `detail` 对象以便父组件获取更多信息[^1]。 --- #### 注意事项 尽管微信小程序支持多种类型的交互行为,但在实际开发中需要注意以下几点: - 并非所有的原生 HTML 元素都完全兼容标准浏览器的行为模式; - 特殊情况下可能需要借助第三方库来弥补平台差异带来的不便之处。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值