微信小程序中target和currentTarget的区别小结
作者:田珊珊个人博客 时间:2024-04-10 16:17:30
前言
在小程序中,e.target与e.currentTarget是非常重要的,尤其是涉及到页面传值时
currentTarget和target都是组件的一些属性值集合,由“data-属性名”定义的一些属性值
currentTarget:事件触发的当前事件(当前事件,可能是触发事件的源组件,可能是触发的事件组件(即触发事件源组件的子元素),此时点击子元素还是父元素,都是当前事件,应用e.currentTarget
target:触发事件的源组件(事件注册/绑定所在组件)
举个例子:
<view bindtap="parentClick" data-parent="父元素">
<view data-child="子元素">编辑</view>
</view>
然后点击父元素中的子元素时:
parentClick事件中的currentTarget和target打印出来,如下:
parentClick:function(e){
console.log("点击父元素")
console.log("target",e.target)//target:{child:"子元素"}
console.log("currentTarget",e.currentTarget)//target:{parent:"父元素"}
}
因为绑定在父元素上的parentClick事件,是由子元素触发的,所以target的值就是子元素所包含的一些属性值集合;currentTarget则是绑定事件的该组件(父元素)的属性值集合。
给子元素绑定一个事件:childClick,打印出来的currentTarget和target就是一样的。
<view bindtap="parentClick" data-parent="父元素">
<view bindtap="childClick" data-child="子元素">编辑</view>
</view>
然后点击子元素事件:
childClick:function(e){
console.log("点击父元素")
console.log("target",e.target)//target:{child:"子元素"}
console.log("currentTarget",e.currentTarget)//target:{child:"子元素"}
}
扩展
结合this来看:
this === e.currentTarget 总是为true
this === e.target 有可能不是true
来源:http://www.tianshan277.com/824.html
标签:微信小程序,target,currenttarget
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
解决django接口无法通过ip进行访问的问题
2023-09-15 14:07:27
![](https://img.aspxhome.com/file/2023/6/62266_0s.jpg)
Java中使用正则表达式的一个简单例子及常用正则分享
2023-05-06 09:03:16
python读取hdfs并返回dataframe教程
2022-03-22 17:15:33
学好python基本数据类型
2023-07-24 21:48:40
使用python进行文本预处理和提取特征的实例
2022-07-13 21:25:47
将设计执行到底—记Qzone项目组视觉设计标注的前前后后
2009-04-19 13:22:00
![](https://img.aspxhome.com/file/UploadPic/20094/19/892_banner-85s.jpg)
bootstrap-table组合表头的实现方法
2024-05-11 09:07:53
![](https://img.aspxhome.com/file/2023/8/127178_0s.png)
对python numpy.array插入一行或一列的方法详解
2022-12-08 15:44:07
Pycharm安装第三方库时Non-zero exit code错误解决办法
2023-03-15 12:15:01
![](https://img.aspxhome.com/file/2023/2/121732_0s.png)
一篇文章带你详细了解python中一些好用的库
2022-03-18 09:02:19
![](https://img.aspxhome.com/file/2023/4/99044_0s.jpg)
详解python uiautomator2 watcher的使用方法
2022-03-04 07:31:23
Tensorflow简单验证码识别应用
2023-08-10 14:13:14
![](https://img.aspxhome.com/file/2023/6/64646_0s.jpg)
asp彩色验证码的制作详解
2007-09-18 13:22:00
深入理解Python装饰器
2023-11-05 13:01:43
python机器学习实现决策树
2021-04-21 07:44:34
![](https://img.aspxhome.com/file/2023/1/100911_0s.jpg)
1行Go代码实现反向代理的示例
2024-04-28 09:15:26
![](https://img.aspxhome.com/file/2023/6/134016_0s.png)
Python lxml库的简单介绍及基本使用讲解
2022-09-12 07:40:55
![](https://img.aspxhome.com/file/2023/3/115623_0s.png)
更新升级python和pip版本后不生效的问题解决
2022-09-30 18:23:14
![](https://img.aspxhome.com/file/2023/5/109905_0s.png)
Go slice切片make生成append追加copy复制示例
2024-02-13 11:48:29
![](https://img.aspxhome.com/file/2023/6/109676_0s.jpg)
微信小程序开发之实现心情记事本
2024-04-18 09:45:20
![](https://img.aspxhome.com/file/2023/0/136280_0s.jpg)