在元素element.click()
但如何使用dblclick
?
javascript中是否存在类似于element.click()
的现有内置函数?
答案 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);