开发者

Triggering double click via jQuery or pure Javascript for a single click event

开发者 https://www.devze.com 2023-03-19 03:17 出处:网络
I want to trigger do开发者_开发知识库uble click event on any element when a single click event occurs in that element.

I want to trigger do开发者_开发知识库uble 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!


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.

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.

var clicknum = 0;
$("#text-box").click(function() {
    clicknum++;
    if (clicknum < 3) {
        $(this).click();
    }
    else {
        // Reset clicknum since we're done.
        clicknum = 0;
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消