javascript 调用 ondblclick 而不调用 onclick-event
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18699677/
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
Call ondblclick without calling onclick-event
提问by rubo77
Is there a simple solution without jquery, inside HTML-tags, that will call the ondblclick
without causing onclick
to happen too?
是否有一个没有 jquery 的简单解决方案,在 HTML 标签中,它也会调用ondblclick
而不导致onclick
发生?
This always closes the window although it should just show the alert:
这总是关闭窗口,尽管它应该只显示警报:
<a href="#" ondblclick="alert('dbl')" onclick="window.close();">X</a>
(it only works in a javascript popup, cause you cannot close the main window with window.close();
)
(它仅适用于 javascript 弹出窗口,因为您无法使用 关闭主窗口window.close();
)
采纳答案by rubo77
My guess is that every solution will also have the problem with the doubleclick timeout-length varying on user preferences described by Irvin Dominin aka Edwardin the comment above:
我的猜测是,每个解决方案也会有双击超时长度的问题,这取决于Irvin Dominin aka Edward在上面的评论中描述的用户偏好:
From jQuery docs but related: "It is inadvisable to bind handlers to both the click and dblclick events for the same element. The sequence of events triggered varies from browser to browser, with some receiving two click events before the dblclick and others only one. Double-click sensitivity (maximum time between clicks that is detected as a double click) can vary by operating system and browser, and is often user-configurable."
来自 jQuery 文档但相关:“将处理程序绑定到同一元素的 click 和 dblclick 事件是不可取的。触发的事件序列因浏览器而异,有些在 dblclick 之前接收两个点击事件,而其他人只接收一个。双击灵敏度(被检测为双击的最大点击间隔时间)可能因操作系统和浏览器而异,并且通常是用户可配置的。”
I abandoned using doubleclick and used CTRL+click instead:
我放弃使用 doubleclick 并使用 CTRL+click 代替:
<a href="#" onclick="if(event.ctrlKey) {
alert('CTRL+Mouseclick'); return false;
} else { window.close(); }">X</a>
see:
看:
回答by Human Being
I have solvedthis with the below code,
我已经用下面的代码解决了这个问题,
<a id="press" href="javascript:void(0)" onclick="singleClick(event)"
ondblclick="doubleClick(event)">Click here</a>
<div id="log"></div>
My JavaScriptwill be ,
我的JavaScript将是,
var timer;
var status = 1;
function singleClick(event) {
status = 1;
timer = setTimeout(function() {
if (status == 1) {
document.getElementById("log").innerHTML ='I am single click !';
}
}, 500);
}
function doubleClick(event) {
clearTimeout(timer);
status = 0;
document.getElementById("log").innerHTML = 'I am a double click!';
}
Please let me know the status.Hope this helps.
请让我知道状态。希望这有帮助。
回答by Irvin Dominin
I think this is a situation difficult to handle, you can use a timeout, but you can't be sure about the correct behaviour.
我认为这是一种难以处理的情况,您可以使用超时,但您无法确定正确的行为。
In the jQuery (but vanilla too) docs there is an important note, that tell us to avoid the handling of both single and double click:
在 jQuery(但也是 vanilla)文档中,有一个重要的说明,告诉我们要避免处理单击和双击:
It is inadvisable to bind handlers to both the click and dblclick events for the same element. The sequence of events triggered varies from browser to browser, with some receiving two click events before the dblclick and others only one. Double-click sensitivity (maximum time between clicks that is detected as a double click) can vary by operating system and browser, and is often user-configurable.
不建议将处理程序绑定到同一元素的 click 和 dblclick 事件。触发事件的顺序因浏览器而异,有些在 dblclick 之前收到两个点击事件,而其他只收到一个。双击灵敏度(被检测为双击的最大点击间隔时间)可能因操作系统和浏览器而异,并且通常是用户可配置的。
So in think that in short:
因此,简而言之:
- if you can refactor your UI with a single handler
- alternatively use a timeout as suggested here, but will not be bulletproof
- 如果您可以使用单个处理程序重构您的 UI
- 或者按照此处的建议使用超时,但不会防弹
回答by Meisner
JS:
JS:
var single;
HTML:
HTML:
<a href="#"
ondblclick="single=0; alert('dbl')"
onclick="single=1; setTimeout(function(){
if (single) window.close();
},300);">X</a>
回答by Hyman
I know it's not what you want to do for your question, but here's a way to use a click and a double easily:
我知道这不是您想要为您的问题做的事情,但这里有一种轻松使用单击和双击的方法:
If you need to select an object with a click (or save a variable for example), you can use javascript to call a function in a "href".
如果您需要通过单击选择一个对象(或例如保存一个变量),您可以使用 javascript 在“href”中调用一个函数。
Then you can use the "ondblclik ()" to directly edit the registry, without clicking the edit button.
然后就可以使用“ondblclik()”直接编辑注册表,无需点击编辑按钮。
The function in the "href" is always executed, so do not test it with "alert ()", you will not get a double click, as the message window will open. It runs 2 times, then perform an action that can be a problem, but not to save or update variables.
“href”中的函数总是被执行,所以不要用“alert()”测试它,你不会得到双击,因为消息窗口会打开。它运行 2 次,然后执行可能有问题的操作,但不保存或更新变量。
function clickMe(c){
var link = document.getElementById('link');
link.innerHTML = "Clicked: "+c;
}
<a id="link" href="javascript:clickMe(1);void(0);" ondblclick="clickMe(2);">Click Me</a>