按钮事件--嵌套事件(冒泡)--带参数事件--双向数据绑定

1
2
<!--按钮事件-->
  <button bindtap="buttonTapHandle">点击事件</button>
 buttonTapHandle: function (e) {
    console.log("我点击了")
    //console.dir(e)将一个对象以树状形式打印到控制台
    console.dir(e)
  },
1
2
3
4
5
6
7
<!--冒泡事件,嵌套事件,防止两个一块执行-->
  <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("内部的事件")
  },
1
2
<!--事件传参-->
 <button bindtap="tap2Handle" data-name="张三">点击事件</button>
 tap2Handle: function (e){
    console.dir(e.target.dataset.name)
    //console.log(this)//事件处理函数中的this指定的还是页面对象
  },
1
2
3
4
5
<!-- 双向数据绑定 -->
  <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>
复制代码

 

 

posted @   小白咚  阅读(874)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示