事件绑定

事件绑定


demo.js文件(逻辑文件,Javascript)

Page({
  /**
   * 页面的初始数据
   */
  data: {
    num:120
  },
  handleInput(e){
    console.log(e.detail.value);
    this.setData({
      num:e.detail.value
    })
  },
  handletap(e){
    //console.log(e);
    // 获取自定义属性 operation
    const operation=e.currentTarget.dataset.operation;
    // setData设置值 this.data.num获取值
    this.setData({
      num:this.data.num + operation
    })
  }
})

demo.wxml文件(配置文件,html)

<!--pages/demo/demo.wxml-->
<!-- 
  1 需要给input标签绑定 input 事假 关键字【bindinput】
  2 通过事件源对象来获取 【e.detail.value】
  3 把输入框的值赋值到 data 中
  错误写法:
  1 this.data.num=e.detail.value
  2 this.num=e.detail.value
  正确写法:
  this.setData({
    num:e.detail.value
  })

  4 添加点击事件
  bindtap 无法在小程序中的 事件中 直接传参
  通过自定义属性的方式来传递参数
  事件源中 获取自定义的属性
 -->
<input type="text" bindinput="handleInput" />
<view>
  {{num}}
</view>
<button bindtap="handletap" data-operation="{{1}}">+</button>
<button bindtap="handletap" data-operation="{{-1}}">-</button>

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值