id="btn"
):<button id="btn" bindtap="handleTap">点我一下</button>
e.target.id
): handleTap(e){ console.log(e.target.id); }
data-type="confirm_btn"
):<button data-type="confirm_btn" bindtap="handleTap">点我一下</button>
e.target.dataset.type
): handleTap(e){ console.log(e.target.dataset.type); }
代码涉及的主要文件有:
{
"pages": [
"pages/index/index"
],
"window": {
"navigationBarBackgroundColor": "#0149af",
"navigationBarTitleText": "登录中心",
"navigationBarTextStyle": "white"
},
"style": "v2",
"sitemapLocation": "sitemap.json"
}
.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;
}
<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>
Page({
data:{
username:"",
password:""
},
handleInput(e){
const type = e.target.id;
this.setData({
[type]:e.detail.value
})
}
})
代码变更涉及到文件有:
<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>
Page({
data:{
username:"",
password:""
},
handleInput(e){
const type = e.target.dataset.type;
this.setData({
[type]:e.detail.value
})
}
})