代码如下:
Object.prototype.longclick=function(time,fun){
if(/Android|webOS|iPhone|iPod|BlackBerry|iPad|IEMobile|Nexus/i.test(navigator.userAgent)){
this.addEventListener('touchstart',function(){
var a=setTimeout(fun,time)
this.addEventListener('touchend',function(){
clearTimeout(a)
})
})
}else{
this.addEventListener('mousedown',function(){
var a=setTimeout(fun,time)
this.addEventListener('mouseup',function(){
clearTimeout(a)
})
})
}
}
调用方法:Element.longclick(时间,事件)
例如:
如果有什么问题也欢迎留言。看到了会及时回复。
同时也欢迎关注我的github:https://github.com/hadardb