如何使用 javascript 双击对象?我必须 .click() 两次吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23926921/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 01:49:23  来源:igfitidea点击:

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

javascriptdouble-clicktampermonkey

提问by Brock Adams

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.

有没有自动双击的方法?我没有使用 jQuery,所以请在答案中避免使用它。

So I click using:

所以我点击使用:

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

I tried double clicking with:

我尝试双击:

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

With no success.

没有成功。

回答by Brock Adams

Dispatch a dblclickeventlike so:

像这样发送一个dblclick事件

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.


您可以在 jsFiddle 上查看代码的运行情况

回答by Jalpesh Vadgama

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

使用 jquery dblclick 方法,您可以非常轻松地做到这一点。就像是。

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

see following link for more information

请参阅以下链接了解更多信息

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

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

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

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

and If you are using javascript then see following link.

如果您使用的是 javascript,请参阅以下链接。

Javascript Double Click Event

Javascript双击事件

回答by jhyap

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