Home > Web Front-end > JS Tutorial > 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

黄舟
Release: 2017-06-27 10:36:46
Original
1477 people have browsed it

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);
                    });
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template