我有一个元素(TextArea)。现在我想要一个长按事件和一个元素上的双击事件。我可以做到这一点,但我也想在长按事件的mousedown事件中使用event.preventDefault()。这反过来也会阻止dblClick事件。
我想要preventDefault的原因是我在longpress上渲染了一个元素,并想防止在长按后触发mouseDown时出现初始的鼠标移动。我在网上找了又找,但没有找到一个好的答案来解决长时间按下和dblclick在同一元素上的问题。
谢谢!!
发布于 2013-10-23 18:46:24
尝试此
HTML
<input type="button" ondblclick="whateverFunc()" onmousedown="func(event)" onmouseup="revert()" value="hold for long"/>JavaScript
var timer;
var istrue = false;
var delay = 3000; // how much long u have to hold click in MS
function func(e)
{
istrue = true;
timer = setTimeout(function(){ makeChange();},delay);
// Incase if you want to prevent Default functionality on mouse down
if (e.preventDefault)
{
e.preventDefault();
} else {
e.returnValue = false;
}
}
function makeChange()
{
if(timer)
clearTimeout(timer);
if(istrue)
{
/// rest of your code
alert('holding');
}
}
function revert()
{
istrue =false;
}
function whateverFunc()
{
alert('dblclick');
}https://stackoverflow.com/questions/19539329
复制相似问题