通过 jQuery 或纯 Javascript 为单击事件触发双击
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6648264/
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
Triggering double click via jQuery or pure Javascript for a single click event
提问by kxhitiz
I want to trigger double click event on any element when a single click event occurs in that element.
当该元素中发生单击事件时,我想在该元素上触发双击事件。
To be more clear, let's say I have a text box with some text, and when the user clicks(single click) on the text box I have to trigger that single click to multiple clicks(either double click or even tripple click).
更清楚地说,假设我有一个带有一些文本的文本框,当用户单击(单击)文本框时,我必须触发单击多次单击(双击甚至三次单击)。
I tried the following way, but in vain :(
我尝试了以下方法,但徒劳无功:(
$('#timer').click(function() {
$('#timer').dblclick();
});
Thanks in advance.
提前致谢。
Cheers!
干杯!
回答by vinod
The code you provided above works for me. A double click is triggered when a single click occurs. I used this variation:
您上面提供的代码对我有用。当单击发生时触发双击。我使用了这个变体:
var numd = 0;
$("#content").dblclick(function() {
numd++;
});
$("#content").click(function() {
$(this).dblclick();
});
numd
is incremented correctly.
numd
正确递增。
For multiple clicks:
对于多次点击:
You could use a variable to keep track of which click you are on while using the click()
method to perform clicks. Here is an example to trigger a triple click.
在使用该click()
方法执行点击时,您可以使用一个变量来跟踪您点击了哪个点击。这是触发三次点击的示例。
var clicknum = 0;
$("#text-box").click(function() {
clicknum++;
if (clicknum < 3) {
$(this).click();
}
else {
// Reset clicknum since we're done.
clicknum = 0;
}
}