听双击而不是点击

发布于 2024-12-12 02:42:36 字数 1333 浏览 0 评论 0原文

我只是想知道为什么当我 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

好菇凉咱不稀罕他 2024-12-19 02:42:37

我怀疑您正在使用速度较慢的计算机。在速度较慢的计算机上,双击可能会被解释为两次单击,中间间隔很长的时间。您可以尝试鼠标设置并更改双击设置。这应该可以解决问题。如果您的计算机速度非常快并且没有延迟问题,那么您的问题可能出在其他地方。双击不太可能被视为代码错误(您发布的那个)的单击。如果不是计算机速度慢的话,问题可能出在其他地方。

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.

倾城花音 2024-12-19 02:42:36

dblclick 并不神奇:虽然第二次快速 click 触发了 dblclick 事件,但第一次 click 已经触发了其自己的事件处理程序。

您几乎不应该在 DOM 元素上同时设置 clickdblclick 事件;当你这样做时,你需要使用计时器来缓解这个问题。

在这种特定情况下,您还需要修复拼写错误 (s/dbclick/dblclick/) 才能触发事件。

另请注意,dblclick 实际上根本不是 DOM 规范的一部分 (不存在于 DOM Level 2 1.6.2)。因此,它被称为“DOM Level 0< /a>”功能。

dblclick is not magical: though the second rapid click fires the dblclick event, the first click has already triggered its own event handler.

You should pretty much never set both a click and a dblclick 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.

岁月蹉跎了容颜 2024-12-19 02:42:36

'dbclick' 更改为 'dblclick'

Change 'dbclick' to 'dblclick'.

风柔一江水 2024-12-19 02:42:36

使用dblclick 而不是dbclick

Use dblclick instead of dbclick.

旧时光的容颜 2024-12-19 02:42:36

要回答修改后的问题(如何相互排斥地处理 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

单挑你×的.吻 2024-12-19 02:42:36

这对我有用(使用 d3 库,但 d 也可以是字典对象):

function log(s){try{console.log(s)}catch(e){}} // for debugging

var click_d = null

function click(d){
    log("click")
    click_d = d
    setTimeout(click_action, 200)
}

function click_action(){
    log("click_action")
    if(click_d == null){
        log("aborted")
        return
    }
    d = click_d

    // do things with d
}

function doubleclick(d){
    log("dblclick")
    click_d = null

    // do things with d
}

This works for me (using the d3 library, but d could also be a dictionary object):

function log(s){try{console.log(s)}catch(e){}} // for debugging

var click_d = null

function click(d){
    log("click")
    click_d = d
    setTimeout(click_action, 200)
}

function click_action(){
    log("click_action")
    if(click_d == null){
        log("aborted")
        return
    }
    d = click_d

    // do things with d
}

function doubleclick(d){
    log("dblclick")
    click_d = null

    // do things with d
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文