<html>
<head>
<script>
function enableLongPress(target, threshold) {
var timer;
var timeOut;
var evt = document.createEvent('Event');
evt.initEvent('longpress', true, true);
target.addEventListener('mousedown', function() {
timer = Date.now();
timeOut=setTimeout(function(){
target.dispatchEvent(evt);
},threshold);
}, false);
target.addEventListener('mouseup', function() {
if(Date.now() - timer < threshold) {
evt.duration = Date.now() - timer;
clearTimeout(timeOut);
}
}, false);
}
</script>
</head>
<body>
<input type="button" name="xx" id="btn1" value="btn1" />
<script>
var button=document.getElementById("btn1");
enableLongPress(button
js自定义"longPress"长按事件
最新推荐文章于 2023-06-28 11:07:35 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)