D3:如何设置“点击"?事件和“dbclick"同时举办活动?

我已经将点击事件切换到一个节点,我也想将一个 dbclick 事件切换到它.但是它只会在我点击它时触发点击事件.

I've toggled click event to a node and I want to toggle a dbclick event to it as well. However it only triggers the click event when I dbclick on it.

那么如何同时设置两个事件呢?

So How do I set both events at the same time?

推荐答案

你必须自己做双击检测

类似的东西可以工作:

var clickedOnce = false;
var timer;

$("#test").bind("click", function(){
    if (clickedOnce) {
        run_on_double_click();
    } else {
        timer = setTimeout(function() {
           run_on_simple_click(parameter);
        }, 150);
        clickedOnce = true;
    }
});

function run_on_simple_click(parameter) {
    alert(parameter);
    alert("simpleclick");
    clickedOnce = false;
}

function run_on_double_click() {
    clickedOnce = false;
    clearTimeout(timer);
    alert("doubleclick");
}

这是一个有效的 JSFiddle

有关您应该为计时器使用什么延迟的更多信息,请查看此处:如何在一个元素上同时使用 onclick 和 ondblclick?

For more information about what delay you should use for your timer, have a look here : How to use both onclick and ondblclick on an element?

相关文章