按钮事件--嵌套事件(冒泡)--带参数事件--双向数据绑定
2023-09-11 14:18:36 时间
<!--按钮事件--> <button bindtap="buttonTapHandle">点击事件</button>
buttonTapHandle: function (e) { console.log("我点击了") //console.dir(e)将一个对象以树状形式打印到控制台 console.dir(e) },
<!--冒泡事件,嵌套事件,防止两个一块执行--> <view bindtap="outerHandle" style="width:200px; height:200px; background-color:red"> <!--<view bindtap="innerHandle" style="width:100px; height:100px; background-color:blue"> </view>--> <view catchtap="innerHandle" style="width:100px; height:100px; background-color:blue"> </view> </view>
outerHandle: function(){ console.log("外部的事件") }, //防止冒泡,将bindtap给为catchtap innerHandle: function(){ console.log("内部的事件") },
<!--事件传参--> <button bindtap="tap2Handle" data-name="张三">点击事件</button>
tap2Handle: function (e){ console.dir(e.target.dataset.name) //console.log(this)//事件处理函数中的this指定的还是页面对象 },
<!-- 双向数据绑定 --> <view> <input value="{{ message2 }}" bindinput="inputHandle" style="border:2px solid #C0C0C0;" /> <text>{{ message2 }}</text> </view>
inputHandle: function (e){ // this.data.message2 = e.detail.value//直接赋值,不能实时改变 //调用setData方法,实时监听改变 this.setData({ message2 : e.detail.value })
index.js
//index.js //获取应用实例 const app = getApp() //将多有的数据和事件写到page方法中 Page({ //为页面提供数据的 //data就是界面和逻辑之间的桥梁 data:{ message:"Hello world", perssion:{ name: "zhangsan", age: 12 }, viewClassname:"hello", todos:[ { name: 'javascript', completed:false }, { name: 'html', completed: true }, { name: 'css', completed: false } ], message2:"", }, buttonTapHandle: function (e) { console.log("我点击了") //console.dir(e)将一个对象以树状形式打印到控制台 console.dir(e) }, outerHandle: function(){ console.log("外部的事件") }, //防止冒泡,将bindtap给为catchtap innerHandle: function(){ console.log("内部的事件") }, tap2Handle: function (e){ console.dir(e.target.dataset.name) //console.log(this)//事件处理函数中的this指定的还是页面对象 }, inputHandle: function (e){ // this.data.message2 = e.detail.value//直接赋值,不能实时改变 //调用setData方法,实时监听改变 this.setData({ message2 : e.detail.value }) } })
index.wxml
<!--index.wxml--> <!-- 基于xml语言,用来定义页面结构单标签也也结束例如image--> <view class="container"> <text>{{message}}</text> <text>{{perssion.name}}</text> <text>{{perssion.age}}</text> <view class=" style1 {{viewClassname}}"></view> <!-- mestach语法可以用在以上,不能用于定义标签名和属性名--> <!--可以直接使用字面量和简单的逻辑运算符--> <!--列表渲染--> <!--起别名wx:for-item="别名"--> <view> <view wx:for="{{ todos }}" wx:key="key"> <text>{{ index }}</text> <checkbox checked="{{ item.completed }}"></checkbox> <text>{{ item.name }}</text> </view> </view> <!--按钮事件--> <button bindtap="buttonTapHandle">点击事件</button> <!--冒泡事件,嵌套事件,防止两个一块执行--> <view bindtap="outerHandle" style="width:200px; height:200px; background-color:red"> <!--<view bindtap="innerHandle" style="width:100px; height:100px; background-color:blue"> </view>--> <view catchtap="innerHandle" style="width:100px; height:100px; background-color:blue"> </view> </view> <!--事件传参--> <button bindtap="tap2Handle" data-name="张三">点击事件</button> <!-- 双向数据绑定 --> <view> <input value="{{ message2 }}" bindinput="inputHandle" style="border:2px solid #C0C0C0;" /> <text>{{ message2 }}</text> </view> </view>
相关文章
- Hadoop2.6.0的事件分类与实现
- js事件之event.preventDefault()与(www.111cn.net)event.stopPropagation()用法区别
- Asp.net(C#) windows 服务{用于实现计划任务,事件监控等}
- el-step给步骤增加单击事件
- Qt音视频开发5-vlc事件订阅
- JavaScript事件处理的例子:事件捕捉和冒泡 - event capture and bubble
- nodejs中事件循环中的执行顺序
- Threejs 的场景查看 - 几个交互事件库助你方便查看场景
- 简搭(jabdp)宝典:新版事件编辑
- 苏宁基于服务层知识图谱的真假告警判定——本质上就是数据标注后 看上报事件和标注的误报、非误报事件相似度来消噪
- e647. 处理鼠标移动事件
- 缩略版muduo网络库(2):事件处理器 Chanel