如何在javascript中创建dblclick事件?

时间:2015-01-14 03:13:40

标签: javascript

在元素element.click()

上调用点击事件

但如何使用dblclick

javascript中是否存在类似于element.click()的现有内置函数?

2 个答案:

答案 0 :(得分:2)

只需使用ondblclick

http://jsfiddle.net/dirtyd77/yrL8r6qd/

var p = document.getElementById('dbl');
p.ondblclick = function (){
  this.innerHTML = 'dbl';  
};
p.ondblclick(); // if you want to "trigger" the event, call it like so

使用addEventListener时,您需要使用dispatchEvent

http://jsfiddle.net/dirtyd77/yrL8r6qd/2/

var p = document.getElementById('dbl');

p.addEventListener('dblclick',function(e){
    this.innerHTML = 'dbl';
});

p.dispatchEvent(new Event('dblclick'));

答案 1 :(得分:1)

object.ondblclick=function(){myScript};

object.addEventListener("dblclick", myScript);