代码编写:
$.fn.longPress = function(fn) {
var timeout = undefined;
var $this = this;
for(var i = 0;i<$this.length;i++){
$this[i].addEventListener('touchstart', function(event) {
timeout = setTimeout(fn, 800);
}, false);
$this[i].addEventListener('touchend', function(event) {
clearTimeout(timeout);
}, false);
}
}
代码使用:
$(select).longPress(function(){
alert(1);
});
本文介绍了一段用于在网页上实现长按事件的JavaScript代码。通过自定义$.fn.longPress函数,开发者可以在需要的地方轻松添加长按事件监听。示例代码展示了如何将此功能应用于已有的HTML元素。
&spm=1001.2101.3001.5002&articleId=98526993&d=1&t=3&u=a054bfccc522416c96dbb00739519fbd)
9万+

被折叠的 条评论
为什么被折叠?



