听双击而不是点击
我只是想知道为什么当我 dbclick
元素时会发生 click
事件?
我有这个代码:(JSBIN)
HTML
<p id="hello">Hello World</p>
JavaScript
document.getElementById('hello').addEventListener('click', function(e){
e.preventDefault();
this.style.background = 'red';
}, false);
document.getElementById('hello').addEventListener('dbclick', function(){
this.style.background = 'yellow';
}, false);
它应该为单击和双击执行不同的操作,但似乎当您双击 p
时它会捕获 click 提前事件并忽略双击。
我也尝试了 preventDefault
click
事件。 我怎样才能只听dbclick
?
更新
我的代码中有一个拼写错误。 dbclick
是错误的。它是dblclick
。无论如何问题仍然存在。当用户双击时,会发生 click
事件。
这是证明这一点的更新代码:(JSBin)
document.getElementById('hello').addEventListener('click', function(e){
e.preventDefault();
this.style.background = 'red';
this.innerText = "Hello World clicked";
}, false);
document.getElementById('hello').addEventListener('dblclick', function(){
this.style.background = 'green';
}, false);
I'm just wondering why click
event happening when I dbclick
an element?
I have this code:(JSBIN)
HTML
<p id="hello">Hello World</p>
JavaScript
document.getElementById('hello').addEventListener('click', function(e){
e.preventDefault();
this.style.background = 'red';
}, false);
document.getElementById('hello').addEventListener('dbclick', function(){
this.style.background = 'yellow';
}, false);
It should do different things for click and double click, but it seems when you double click on the p
it catch click
event in advance and ignore double click.
I tried preventDefault
the click
event too.
How can I listen to just dbclick
?
UPDATE
I had a typo in my code. dbclick
is wrong. It's dblclick
. Anyway the problem still exist. When user double clicks the click
event happens.
This is updated code that prove it:(JSBin)
document.getElementById('hello').addEventListener('click', function(e){
e.preventDefault();
this.style.background = 'red';
this.innerText = "Hello World clicked";
}, false);
document.getElementById('hello').addEventListener('dblclick', function(){
this.style.background = 'green';
}, false);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我怀疑您正在使用速度较慢的计算机。在速度较慢的计算机上,双击可能会被解释为两次单击,中间间隔很长的时间。您可以尝试鼠标设置并更改双击设置。这应该可以解决问题。如果您的计算机速度非常快并且没有延迟问题,那么您的问题可能出在其他地方。双击不太可能被视为代码错误(您发布的那个)的单击。如果不是计算机速度慢的话,问题可能出在其他地方。
I would suspect you are working on slow computer. On a slow computer double click could be interpreted as two single click with significant time in between. You can experiment with mouse setting and change the double click setting. That should troubleshoot the problem. If you are computer is really fast and has no lag issue, your problem could be somewhere else. It is very unlikely that double click could be taken as single click as code bug (the one you posted). Problem could be elsewhere if not slowness of the computer.
dblclick
并不神奇:虽然第二次快速click
触发了dblclick
事件,但第一次click
已经触发了其自己的事件处理程序。您几乎不应该在 DOM 元素上同时设置
click
和dblclick
事件;当你这样做时,你需要使用计时器来缓解这个问题。在这种特定情况下,您还需要修复拼写错误 (
s/dbclick/dblclick/
) 才能触发事件。另请注意,
dblclick
实际上根本不是 DOM 规范的一部分 (不存在于 DOM Level 2 1.6.2)。因此,它被称为“DOM Level 0< /a>”功能。dblclick
is not magical: though the second rapidclick
fires thedblclick
event, the firstclick
has already triggered its own event handler.You should pretty much never set both a
click
and adblclick
event on a DOM element; when you do, you'll need fancy tricks with timers to mitigate the issue.In this specific scenario, you'll also need to fix your typo (
s/dbclick/dblclick/
) to get the event to fire at all.Also note that
dblclick
is not actually part of the DOM specification at all (not present in DOM Level 2 1.6.2). For this reason it's known as a "DOM Level 0" feature.将
'dbclick'
更改为'dblclick'
。Change
'dbclick'
to'dblclick'
.使用
dblclick
而不是dbclick
。Use
dblclick
instead ofdbclick
.要回答修改后的问题(如何相互排斥地处理 click 和 dblclick),您必须延迟 click 事件,直到不再可能执行 dblclick。这给点击处理带来了轻微的延迟(例如,500 毫秒),但 DOM 无法预测第二次点击是否会到达。
此答案中有一个示例脚本:https://stackoverflow.com/a/11057483/43217
To answer the revised question (How to mutually exclusively handle click and dblclick) you have to delay the click event until dblclick is no longer possible. This gives a slight lag (e.g., 500ms) to click handling but otherwise there is no way for the DOM to predict whether a second click will be arriving.
An example script is in this answer: https://stackoverflow.com/a/11057483/43217
这对我有用(使用 d3 库,但 d 也可以是字典对象):
This works for me (using the d3 library, but d could also be a dictionary object):