当前位置:网站首页>微信小程序中给event对象传递数据
微信小程序中给event对象传递数据
2022-07-06 09:53:00 【richest_qi】
文章目录
前情提要
给event对象传递id
- 给event对象传递id(
id="btn"
):<button id="btn" bindtap="handleTap">点我一下</button>
- 从event对象中获取id(
e.target.id
):handleTap(e){ console.log(e.target.id); }
给event对象传递自定义属性
- 给event对象传递自定义属性(
data-type="confirm_btn"
):<button data-type="confirm_btn" bindtap="handleTap">点我一下</button>
- 从event对象中获取自定义属性(
e.target.dataset.type
):handleTap(e){ console.log(e.target.dataset.type); }
小程序项目
向event对象传递id
代码涉及的主要文件有:
- app.json
- pages/index/index.wxss
- pages/index/index.wxml
- pages/index/index.js
app.json
{
"pages": [
"pages/index/index"
],
"window": {
"navigationBarBackgroundColor": "#0149af",
"navigationBarTitleText": "登录中心",
"navigationBarTextStyle": "white"
},
"style": "v2",
"sitemapLocation": "sitemap.json"
}
pages/index/index.wxss
.container{
padding: 20rpx;
}
.container .title{
padding: 20rpx 0;
border-bottom: 1px solid #ddd;
font-weight: bold;
font-size: 32rpx;
}
.list{
margin: 40rpx auto 20rpx;
}
.item{
margin: 12rpx 0;
padding: 0 20rpx;
border: 1px solid #ddd;
border-radius: 6rpx;
}
.item input{
width: 100%;
height: 60rpx;
font-size: 28rpx;
}
button{
font-weight: normal;
font-size: 28rpx!important;
color: #fff;
background: #0149af;
}
pages/index/index.wxml
<view class="container">
<view class="title">登录</view>
<view class="list">
<view class="item">
<input type="text" id="username" bindinput="handleInput" placeholder="用户名" />
</view>
<view class="item">
<input type="text" password id="password" bindinput="handleInput" placeholder="密码" />
</view>
</view>
<button size="mini">登录</button>
</view>
pages/index/index.js
Page({
data:{
username:"",
password:""
},
handleInput(e){
const type = e.target.id;
this.setData({
[type]:e.detail.value
})
}
})
向event对象传递自定义属性
代码变更涉及到文件有:
- pages/index/index.wxml
- pages/index/index.js
pages/index/index.wxml
<view class="container">
<view class="title">登录</view>
<view class="list">
<view class="item">
<input type="text" data-type="username" bindinput="handleInput" placeholder="用户名" />
</view>
<view class="item">
<input type="text" password data-type="password" bindinput="handleInput" placeholder="密码" />
</view>
</view>
<button size="mini">登录</button>
</view>
pages/index/index.js
Page({
data:{
username:"",
password:""
},
handleInput(e){
const type = e.target.dataset.type;
this.setData({
[type]:e.detail.value
})
}
})
相关链接
边栏推荐
- Interview assault 63: how to remove duplication in MySQL?
- Solrcloud related commands
- Flet教程之 13 ListView最常用的滚动控件 基础入门(教程含源码)
- Xin'an Second Edition: Chapter 24 industrial control safety demand analysis and safety protection engineering learning notes
- Sqoop I have everything you want
- Huawei certified cloud computing hica
- 78 岁华科教授逐梦 40 载,国产数据库达梦冲刺 IPO
- Error: Publish of Process project to Orchestrator failed. The operation has timed out.
- 2021-03-22 "display login screen during recovery" can't be canceled. The appearance of lock screen interface leads to the solution that the remotely connected virtual machine can't work normally
- C version selenium operation chrome full screen mode display (F11)
猜你喜欢
分布式不来点网关都说不过去
中移动、蚂蚁、顺丰、兴盛优选技术专家,带你了解架构稳定性保障
Automatic operation and maintenance sharp weapon ansible Foundation
The problem of "syntax error" when uipath executes insert statement is solved
视频融合云平台EasyCVR增加多级分组,可灵活管理接入设备
[ASM] introduction and use of bytecode operation classwriter class
Flink parsing (VII): time window
Chrome prompts the solution of "your company management" (the startup page is bound to the company's official website and cannot be modified)
Virtual machine startup prompt probing EDD (edd=off to disable) error
【Elastic】Elastic缺少xpack无法创建模板 unknown setting index.lifecycle.name index.lifecycle.rollover_alias
随机推荐
Grafana 9 正式发布,更易用,更酷炫了!
How uipath determines that an object is null
TCP connection is more than communicating with TCP protocol
VR panoramic wedding helps couples record romantic and beautiful scenes
10 advanced concepts that must be understood in learning SQL
Binary search strategy
Chrome prompts the solution of "your company management" (the startup page is bound to the company's official website and cannot be modified)
Kernel link script parsing
【MySQL入门】第三话 · MySQL中常见的数据类型
VR全景婚礼,帮助新人记录浪漫且美好的场景
Summary of Android interview questions of Dachang in 2022 (I) (including answers)
After entering Alibaba for the interview and returning with a salary of 35K, I summarized an interview question of Alibaba test engineer
面试突击62:group by 有哪些注意事项?
EasyCVR接入设备开启音频后,视频无法正常播放是什么原因?
Alertmanager sends the alarm email and specifies it as the Alibaba mailbox of the company
Automatic operation and maintenance sharp weapon ansible Playbook
Openharmony developer documentation open source project
Xin'an Second Edition: Chapter 23 cloud computing security requirements analysis and security protection engineering learning notes
Appium automated test scroll and drag_ and_ Drop slides according to element position
Basic configuration and use of spark