小程序26-事件绑定和事件对象

小程序中绑定事件与网页绑定事件几乎一致,只不过小程序中不能通过 on 的方式绑定事件,也没有 click 等事件,小程序中绑定事件使用 bind 方法,click 事件也需要使用 tap 事件进行代替

方法1: bind:事件名,bind 后面需要跟上冒号,冒号后面跟上事件名,例如:<view bind:tap="fnName"></view>

方法2: bind事件名,bind 后面直接跟事件名,例如:<view bindtap="fnName"></view>

事件处理函数需要写到 .js 文件中,在.js文件中需要调用小程序提供的 Page 方法 来注册小程序的页面,我们可以直接在 Page 方法中创建事件处理函数

注意:小程序中,input 输入框默认没有边框,需要自己添加样式

在app.scss文件中添加:input { border: 1px solid #cccccc; }

html 复制代码
<!-- 第一种方式:bind:事件名 -->
<button type="primary" bind:tap="handler">绑定事件</button>
<!-- 第二种方式:bind事件名 -->
<button type="warn" bindtap="handler">绑定事件</button>

<!-- 小程序中,input 输入框默认没有边框,需要自己添加样式 -->
<input type="text" bindinput="getInputVal"/>
javascript 复制代码
Page({
  handler() {
    console.log('绑定成功')
  },
  // event:事件对象
  getInputVal(event) {
    console.log(event)
    console.log(event.detail.value)
  }
})
相关推荐
我是伪码农3 小时前
Vue 1.23
前端·javascript·vue.js
毕设源码-郭学长8 小时前
【开题答辩全过程】以 基于Web的高校课程目标达成度系统设计与实现为例,包含答辩的问题和答案
前端
wuhen_n8 小时前
高阶函数与泛型函数的类型体操
前端·javascript·typescript
ヤ鬧鬧o.9 小时前
多彩背景切换演示
前端·css·html·html5
lethelyh10 小时前
Vue day1
前端·javascript·vue.js
酉鬼女又兒10 小时前
SQL113+114 更新记录(一)(二)+更新数据知识总结
java·服务器·前端
无风听海10 小时前
AngularJS中 then catch finally 的语义、执行规则与推荐写法
前端·javascript·angular.js
利刃大大10 小时前
【Vue】组件化 && 组件的注册 && App.vue
前端·javascript·vue.js
一起养小猫10 小时前
Flutter for OpenHarmony 实战:按钮类 Widget 完全指南
前端·javascript·flutter
css趣多多10 小时前
Vux store实例的模块化管理
前端