方式一
wxml 片段
<view class="{{longPressTag==index?'long-press-status':''}}
wx:for="{{searchHistory}}" wx:key="index" data-index="{{index}}"
catchtap="onTouch"
bindlongpress="longPress">
{{item}}
</view>
js 片段
Page({
onTouch: function(e) {
},
longPress: function(e) {
}
})
解析
tap
触摸事件采用catch
阻止事件冒泡- 1.5.0之后支持
longpress
事件,手指触摸后,超过350ms再离开,如果指定了事件回调函数并触发了这个事件,tap事件将不被触发
方式二(不推荐)
longtap
事件,但在触发时会同时触发单击事件,需配合touchstart
和touchend
事件计算触屏开始和结束时间来使用
wxml
<view class="{{longPressTag==index?'long-press-status':''}}
wx:for="{{searchHistory}}" wx:key="index" data-index="{{index}}"
catchtap="onTouch"
bindlongtap="longPress"
bindtouchstart="touchStart"
bindtouchend="touchEnd">
{{item}}
</view>
js
Page({
touchStart: function (e) {
this.setData({start:e.timeStamp })
},
touchEnd: function (e) {
this.setData({end: e.timeStamp })
},
longPress: function(e) {
},
onTouch:function(e) {
if (this.data.end - this.data.start < 350){
}
}
})