Home  >  Article  >  Web Front-end  >  An example of adding click and dblclick events at the same time in jQuery

An example of adding click and dblclick events at the same time in jQuery

黄舟
黄舟Original
2017-06-27 10:36:461396browse

Add The code for event is relatively simple. There are two methods:

  • ##$("abc").bind({" click":fn,"dblclick":fn});

  • $("abc").click(fn).dblclick(fn)

The current problem is that no matter double-clicking or clicking, only the click function is executed. Why?

Let’s talk about the double-click mechanism below:

The process of double-click (dblclick) is: mousedown, mouseout, click, mousedown, mouseout, click, dblclick;

To realize double-click, we These two clicks must be blocked, so we set a

timer in the click to delay the execution of the function. The complete code is as follows:

//绑定点击和双击事件
                    var _time = null;
                    $(this).find("tr").dblclick(function(e){
                        clearTimeout(_time);
                        console.log("dblclick");
                        //真正双击代码

                    }).click(function(e){
                        clearTimeout(_time);
                        _time = setTimeout(function(){
                            console.log("click");
                            //单击事件在这里

                        }, 300);
                    });

The above is the detailed content of An example of adding click and dblclick events at the same time in jQuery. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn