javascript:长按一下书签

tic*_*tic 4 javascript bookmarklet click

我需要识别JavaScript小书签中的长按.所以,我不能使用jQuery,也不能使用onclick()事件和类似的东西.有可能,怎么样?

Mic*_*ski 7

onmousedown,呼叫setTimeout()你长时间点击的持续时间.如果允许超时到期,它将调用其函数来执行您希望在长按时执行的任何操作.但是,如果尚未过期onmouseup,setTimeout()则取消.

<script type='text/javascript'>
// t will hold the setTimeout id
// Click for 1 second to see the alert.
var t;
</script>

<button onmousedown='t=setTimeout(function(){alert("hi");}, 1000);' onmouseup='clearTimeout(t);'>Clickme</button>
Run Code Online (Sandbox Code Playgroud)

这里是jsfiddle的行动