当前位置:网站首页>【微信小程序】事件绑定,你搞懂了吗?
【微信小程序】事件绑定,你搞懂了吗?
2022-06-26 15:12:00 【热爱编程的小白白】
目录
WXML 模板语法 - 事件绑定
1. 什么是事件

2. 小程序中常用的事件
类型 | 绑定方式 | 事件描述 |
tap | bindtap 或 bind:tap | 手指触摸后马上离开,类似于 HTML 中的 click 事件 |
input | bindinput 或 bind:input | 文本框的输入事件 |
change | bindchange 或 bind:change | 状态改变时触发 |
🥭3. 事件对象的属性列表
属性 | 类型 | 说明 |
type | String | 事件类型 |
timeStamp | Integer | 页面打开到触发事件所经过的毫秒数 |
target | Object | 触发事件的组件的一些属性值集合 |
currentTarget | Object | 当前组件的一些属性值集合 |
detail | Object | 额外的信息 |
touches | Array | 触摸事件,当前停留在屏幕中的触摸点信息的数组 |
changedTouches | Array | 触摸事件,当前变化的触摸点信息的数组 |
4. target 和 currentTarget 的区别
target 是触发该事件的源头组件,而 currentTarget 则是当前事件所绑定的组件。举例如下:
<view class="out-view" bindtap="outerHandler">
view 组件
<button type="primary">按钮</button>
</view>
.out-view {
height: 100px;
background-color: rgb(15, 218, 201);
}
5. bindtap 的语法格式
<button type="primary" bindtap="bindtap">按钮</button>
Page({
bindtap(e){
console.log(e)
}
})
未点击按钮时:
点击按钮后:
6. 事件处理函数中为数据赋值
通过调用 this.setData(dataObject) 方法,可以给页面 data 中的数据重新赋值,示例如下:
JS:
Page({
data:{
count:0
},
changeCount(){
this.setData({
count:this.data.count+1
})
}
})
WXML:
<button type="primary" bindtap="changeCount">点击值+1</button>
效果:点击按钮,数据+1
7. 事件传参

<button bindtap="btn" data-indo="{
{3}}">事件传参</button>
Page({
data:{
count:0
},
btn(event){
console.log(event.target.dataset)
console.log(event.target.dataset.info)
this.setData({
count:this.data.count+this.data.count+event.target.dataset.info
})
}
})
WXML:
<button bindtap="btn" data-info="{
{3}}">事件传参</button>
未点击按钮:

🧅8. bindinput 的语法格式
<input bindinput="input"/>
Page({
input(e){
console.log(e.detail.value)
}
})
9. 实现文本框和 data 之间的数据同步
Page({
data: {
msg:'加油'
},
input(e){
console.log(e.detail.value)
}
})
<input value="{
{msg}}" bindinput="input"/>
美化样式
input {
width: 100%;
height: 54px;
border-radius: 10px;
background: #e0e0e0;
box-shadow: 20px 20px 60px #bebebe,
-20px -20px 60px #ffffff;
}
边栏推荐
- 【ceph】CephFS 内部实现(二):示例--未消化
- shell脚本多进程并发写法实例(高阶修炼)
- The tablestack function of the epidisplay package of R language makes a statistical summary table (descriptive statistics of groups, hypothesis test, etc.), does not set the by parameter to calculate
- TS common data types summary
- Seurat转h5ad总结
- 杜老师说网站更新图解
- 1. accounting basis -- several major elements of accounting (general accounting theory, accounting subjects and accounts)
- Pod scheduling of kubernetes
- How to load the contour CAD drawing of the engineering coordinate system obtained by the designer into the new earth
- 同花顺注册开户安全吗,有没有什么风险?
猜你喜欢
ETL过程中数据精度不准确问题
设计人员拿到的工程坐标系等高线CAD图如何加载进图新地球
【小程序实战系列】小程序框架 页面注册 生命周期 介绍
【ceph】CEPHFS 内部实现(一):概念篇--未消化
10 minutes to understand bim+gis fusion, common BIM data formats and characteristics
How to load the contour CAD drawing of the engineering coordinate system obtained by the designer into the new earth
Using restcloud ETL shell component to schedule dataX offline tasks
【TcaplusDB知识库】TcaplusDB单据受理-事务执行介绍
在校生学习生涯总结(2022)
Document 1
随机推荐
Sikuli automatic testing technology based on pattern recognition
整理了一批脚本标准的函数模块(2021版)
The R language cartools package divides data, the scale function scales data, and the KNN function of the class package constructs a k-nearest neighbor classifier
【ceph】mkdir|mksnap流程源码分析|锁状态切换实例
vue中缓存页面 keepAlive使用
Analysis of ble packet capturing debugging information
JS之事件
RestCloud ETL解决shell脚本参数化
TCP 复位攻击原理
2Gcsv文件打不开怎么处理,使用byzer工具
Mr. Du said that the website was updated with illustrations
Principle of TCP reset attack
1.会计基础--会计的几大要素(会计总论、会计科目和账户)
SAP GUI 770 Download
The tablestack function of the epidisplay package of R language makes a statistical summary table (descriptive statistics of groups, hypothesis test, etc.), does not set the by parameter to calculate
Applicable and inapplicable scenarios of mongodb series
Database - sequence
redis集群的重新分片与ASK命令
vsomeip3 双机通信文件配置
Pod scheduling of kubernetes