当前位置:网站首页>微信小程序中给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
})
}
})
相关链接
边栏推荐
- Virtual machine startup prompt probing EDD (edd=off to disable) error
- 10 advanced concepts that must be understood in learning SQL
- 分布式(一致性协议)之领导人选举( DotNext.Net.Cluster 实现Raft 选举 )
- Alibaba brand data bank: introduction to the most complete data bank
- Start job: operation returned an invalid status code 'badrequst' or 'forbidden‘
- Pytest learning ----- pytest confitest of interface automation test Py file details
- The most complete tcpdump and Wireshark packet capturing practice in the whole network
- Unity tips - draw aiming Center
- How uipath determines that an object is null
- Optimization of middle alignment of loading style of device player in easycvr electronic map
猜你喜欢
FlutterWeb瀏覽器刷新後無法回退的解决方案
[introduction to MySQL] third, common data types in MySQL
面试突击62:group by 有哪些注意事项?
C version selenium operation chrome full screen mode display (F11)
EasyCVR接入设备开启音频后,视频无法正常播放是什么原因?
Zen integration nails, bugs, needs, etc. are reminded by nails
Pourquoi Li shufu a - t - il construit son téléphone portable?
Interview shock 62: what are the precautions for group by?
[rapid environment construction] openharmony 10 minute tutorial (cub pie)
【MySQL入门】第四话 · 和kiko一起探索MySQL中的运算符
随机推荐
The solution to the left-right sliding conflict caused by nesting Baidu MapView in the fragment of viewpager
The problem of "syntax error" when uipath executes insert statement is solved
视频融合云平台EasyCVR增加多级分组,可灵活管理接入设备
偷窃他人漏洞报告变卖成副业,漏洞赏金平台出“内鬼”
The art of Engineering (1): try to package things that do not need to be exposed
Pourquoi Li shufu a - t - il construit son téléphone portable?
Interview assault 63: how to remove duplication in MySQL?
[ASM] introduction and use of bytecode operation classwriter class
Getting started with pytest ----- allow generate report
Flet教程之 13 ListView最常用的滚动控件 基础入门(教程含源码)
FlutterWeb瀏覽器刷新後無法回退的解决方案
Summary of Android interview questions of Dachang in 2022 (II) (including answers)
Binary search strategy
重磅!蚂蚁开源可信隐私计算框架“隐语”,主流技术灵活组装、开发者友好分层设计...
Cool Lehman has a variety of AI digital human images to create a vr virtual exhibition hall with a sense of technology
After entering Alibaba for the interview and returning with a salary of 35K, I summarized an interview question of Alibaba test engineer
node の SQLite
Debug and run the first xv6 program
Manifest of SAP ui5 framework json
Compile and build, from the bottom to the top