javascript jquery:如何禁用 dblclick 事件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13376601/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jquery: how to disable dblclick events?
提问by Giorgio Robino
premise: developing a graphic scatterplot (with flotchart.org) I have a js function that dynamically create a that show a image (button) to realize a user action CLICKING on the button ("pan Left" on the example code here below)
前提:开发图形散点图(使用 flotchart.org)我有一个 js 函数,它动态创建一个显示图像(按钮)以实现用户点击按钮的操作(下面示例代码中的“向左平移”)
problem: when the user click fast over the button, an (undesired) double click event is triggered. How can I disable double click when mouse is over the button (so allowing only single click event) ? In other words: What wrong using unbind or dblclick inthe code here below ?
问题:当用户在按钮上快速单击时,会触发(不需要的)双击事件。当鼠标悬停在按钮上时如何禁用双击(因此只允许单击事件)?换句话说:在下面的代码中使用 unbind 或 dblclick 有什么问题?
function addButtonPanLeft(x, top, offset) {
$('<img id="buttonPanLeft" class="navigationbutton" src="../images/pan-left.png" style="left:' + x + 'px;top:' + top + 'px"' + ' title="Pan Left">').appendTo(placeholder).click(function(e) {
e.preventDefault();
panleft();
});
// disabilito double click sul bottone
$('#buttonPanLeft').unbind('dblclick');
}
function addButtonPanRight(x, top, offset) {
$('<img id="buttonPanRight" class="navigationbutton" src="../images/pan-right.png" style="left:' + x + 'px;top:' + top + 'px"' + ' title="Pan Right">').appendTo(placeholder).click(function(e) {
e.preventDefault();
panright();
});
// disabilito double click sul bottone NON FUNZIONA ??????
$('#buttonPanRight').dblclick(function(e) {
e.preventDefault();
panright();
log({
text: "double click",
type: 'debug'
});
});
}
many thanks giorgio
非常感谢乔治
回答by Asad Saeeduddin
EDIT: As @Accountant ?has mentioned in the comments, unbind
is now deprecated. Use the similar off
instead.
编辑:作为@Accountant ?已在评论中提到,unbind
现已弃用。改用类似的off
。
unbind
removes all event handlers, it does not prevent the event from being triggered. What you need to do is attachan event handler that stops the event's propagation:
unbind
删除所有事件处理程序,它不会阻止事件被触发。您需要做的是附加一个停止事件传播的事件处理程序:
$('#buttonPanLeft').unbind('dblclick');
$('#buttonPanLeft').dblclick(function(e){
e.stopPropagation();
e.preventDefault();
return false;
});
EDIT: I might have misunderstood your question. In the event that you want to prevent the second click of the double click from firing the click
handler, you can do something like this:
编辑:我可能误解了你的问题。如果您想阻止双击的第二次点击触发click
处理程序,您可以执行以下操作:
function handler(e){
e.preventDefault();
panleft();
$(this).unbind('click');
setTimeout(function(){$('#buttonPanLeft').click(handler)}, 500)
}
$('#buttonPanLeft').click(handler);
Which prevents another click event from taking place until 500 milliseconds have elapsed.
这可以防止另一个点击事件发生,直到 500 毫秒过去。