赞
踩
当某个事件触发时 , 会产生一个 事件对象 , 并且这个对象被传入到回调函数中 , 事件对象有哪些常见的属性呢?
当界面产生一个事件时,事件分为了 捕获阶段 和 冒泡阶段 。
home.wxml:
<!--pages/home/home.wxml--> <!-- 1.事件处理的回顾 --> <button bind:tap="handleBtnClick" size="mini">按钮</button> <button bind:tap="handleBtnClick" size="mini">按钮</button> <button catch:tap="handleBtnClick" size="mini">按钮</button> <!-- 2.常见的一些事件 --> <view class="box" bind:touchstart="handleTouchStart"bind:touchmove="handleTouchMove" bind:touchend="handleTouchEnd" bind:tap="handleTap" bind:longpress="handleLongPress"></view> <!-- 3.事件对象的分析 --> <button id="btn" size="mini" bindtouchend="handleEventEnd" bindtap="handleEventClick">事件对象</button> <view class="outer" id="outer" bindtap="handleOuter"> 外层的view <view class="inner" id="inner" bindtap="handleInner">内层的view</view> </view> <!-- 4.事件的参数传递 --> <view class="container"> <block wx:for="{{titles}}" wx:key="{{index}}"> <view class="item" bindtap="handleItemClick" data-index="{{index}}" data-item="{{item}}">{{item}}</view> </block> </view> <!-- 5.事件冒泡和事件捕获、catch和bind的区别 --> <!-- bind:一层一层传递 --> <!-- catch:阻止事件的进一步传递 --> <view class="view1" capture-bind:tap="handleCaptureView1" bindtap="handleBindView1"> <view class="view2" capture-catch:tap="handleCaptureView2" bindtap="handleBindView2"> <view class="view3" capture-bind:tap="handleCaptureView3" bindtap="handleBindView3"></view> </view> </view>
home.wxss:
/* pages/home/home.wxss */ .title{ color:red; font-size: 30px; } .box{ width: 300rpx; height: 300rpx; background-color: orange; } .outer{ width: 400rpx; height: 400rpx; background-color: orange; } .inner{ width: 200rpx; height: 200rpx; background-color: palegreen; } .view1{ width: 600rpx; height: 600rpx; background-color: palegreen; } .view2{ width: 400rpx; height: 400rpx; background-color: paleturquoise; } .view3{ width: 200rpx; height: 200rpx; background-color: papayawhip; }
home.js:
// pages/home/home.js Page({ data: { titles: ["衣服", "裤子", "鞋子"] }, handleBtnClick() { console.log("按钮发生点击"); }, handleTouchStart() { console.log("handleTouchStart"); }, handleTouchMove() { console.log("handleTouchMove"); }, handleTouchEnd() { console.log("handleTouchEnd"); }, handleTap() { console.log("handleTap"); }, handleLongPress() { console.log("handleLongPress"); }, handleEventClick(event) { console.log("----------", event) }, handleEventEnd(event) { console.log("++++++++++++", event); }, handleInner(event) { console.log(event) }, handleOuter(event) { console.log(event) }, handleItemClick(event) { console.log(event) const dataset = event.currentTarget.dataset; const index = dataset.index; const title = dataset.item; console.log(dataset); console.log(index); console.log(title); }, //事件冒泡和事件捕获 handleCaptureView1() { console.log("handleCaptureView1"); }, handleBindView1() { console.log("handleBindView1"); }, handleCaptureView2() { console.log("handleCaptureView2"); }, handleBindView2() { console.log("handleBindView2"); }, handleCaptureView3() { console.log("handleCaptureView3"); }, handleBindView3() { console.log("handleBindView3"); }, })
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。