How do I double-click on objects using javascript? Do I have to .click() twice?

99封情书 提交于 2019-12-07 07:51:10

问题


I tried searching for this but the only results that come up are on double click, and never how to automatically double click on something. (trigger a double-click.)

I can click once, and have tried doing so twice to "Create" a double-click but failed. I'm assuming that it's because of the timing which is why I set up a timer to see how much time there is between my double clicks.

Is there an automated way to double click? I'm not using jQuery so please avoid it in the answer.

So I click using:

document.getElementyById("somethinbg").click();

I tried double clicking with:

document.getElementById("something").dblclick(); 

With no success.


回答1:


Dispatch a dblclick event like so:

var targLink    = document.getElementById ("something");
var clickEvent  = document.createEvent ('MouseEvents');
clickEvent.initEvent ('dblclick', true, true);
targLink.dispatchEvent (clickEvent);


You can see the code in action at jsFiddle.




回答2:


With jquery dblclick method you can that very easily. Some thing like.

$("p").dblclick(function(){
   alert("The paragraph was double-clicked.");
});

see following link for more information

http://www.w3schools.com/jquery/event_dblclick.asp

http://api.jquery.com/dblclick/

and If you are using javascript then see following link.

Javascript Double Click Event




回答3:


document.getElementById("something").ondblclick = function(){
       alert("The paragraph was double-clicked.");
}


来源:https://stackoverflow.com/questions/23926921/how-do-i-double-click-on-objects-using-javascript-do-i-have-to-click-twice

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!