当前位置:网站首页>【微信小程序】事件绑定,你搞懂了吗?
【微信小程序】事件绑定,你搞懂了吗?
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;
}

边栏推荐
- 一键分析硬件/IO/全国网络性能脚本(强推)
- shell脚本多进程并发写法实例(高阶修炼)
- Applicable and inapplicable scenarios of mongodb series
- 整理了一批脚本标准的函数模块(2021版)
- Execution of commands in the cluster
- 【ceph】CephFS 内部实现(三):快照
- Notes on brushing questions (19) -- binary tree: modification and construction of binary search tree
- 小程序:uniapp解决 vendor.js 体积过大的问题
- 数据库-完整性约束
- Unity C # e-learning (VIII) -- www
猜你喜欢

数据库-序列

【TcaplusDB知识库】TcaplusDB单据受理-建表审批介绍
![[tcapulusdb knowledge base] tcapulusdb system user group introduction](/img/7b/8c4f1549054ee8c0184495d9e8e378.png)
[tcapulusdb knowledge base] tcapulusdb system user group introduction

【ceph】CEPHFS 内部实现(一):概念篇--未消化
![[tcapulusdb knowledge base] tcapulusdb operation and maintenance doc introduction](/img/7b/8c4f1549054ee8c0184495d9e8e378.png)
[tcapulusdb knowledge base] tcapulusdb operation and maintenance doc introduction

TS common data types summary

Halcon C# 设置窗体字体,自适应显示图片

【TcaplusDB知识库】TcaplusDB单据受理-创建游戏区介绍

使用RestCloud ETL Shell组件实现定时调度DataX离线任务

Function: crypto JS encryption and decryption
随机推荐
Shell script multi process concurrent writing method example (high level cultivation)
数据库-视图
Pod of kubernetes
Database - sequence
Database - integrity constraints
设计人员拿到的工程坐标系等高线CAD图如何加载进图新地球
Is it safe to open a new bond registration account? Is there any risk?
Execution of commands in the cluster
R language dplyr package summary_ The at function calculates the mean and median of multiple data columns (specified by vectors) in the dataframe data, and specifies na RM parameter configuration dele
【TcaplusDB知识库】TcaplusDB单据受理-创建游戏区介绍
JS之简易deepCopy(简介递归)
[tcapulusdb knowledge base] tcapulusdb doc acceptance - table creation approval introduction
在校生学习生涯总结(2022)
【ceph】cephfs caps简介
Is it safe to open a stock account through the account opening link of the broker manager? Or is it safe to open an account in a securities company?
shell脚本多进程并发写法实例(高阶修炼)
[tcapulusdb knowledge base] tcapulusdb doc acceptance - create business introduction
Sikuli automatic testing technology based on pattern recognition
RestCloud ETL解决shell脚本参数化
R language uses the aggregate function of epidisplay package to split numerical variables into different subsets based on factor variables, calculate the summary statistics of each subset, and use agg