Home > Article > Web Front-end > How to make only double click event happen in jquery
In Web development, jquery is often used to achieve page interaction effects. Among them, double-click events and click events are inevitable. But sometimes it is found that a double-click event triggers two click events. At this time, we need to prevent the click event from occurring and only allow the double-click event to occur. So how should it be achieved?
First, we need to understand the event binding method in jquery. There are two methods for event binding, one is to bind using the bind() method, and the other is to bind using the on() method. These two methods are essentially the same. The on() method is an upgraded version of the bind() method, adding support for dynamic elements. We can implement double-click event binding through these two methods.
The specific implementation method is as follows:
Use the bind() method to bind double-click events and click events.
$("#element").bind({ click: function() { //单击事件的处理逻辑 }, dblclick: function() { //双击事件的处理逻辑 } });
This method actually defines an object and binds click and dblclick events. Then their corresponding processing logic functions are given respectively.
Use the on() method to bind double-click events and click events.
$("#element").on("click", function() { //单击事件的处理逻辑 }).on("dblclick", function() { //双击事件的处理逻辑 });
This method first binds the click event, and then binds the dblclick event in a chain. It is also necessary to give their corresponding processing logic functions respectively.
In the above two methods, we can notice that the double-click event is bound after the click event. This is because if the double-click event is bound before the click event, then when the user double-clicks, the double-click event will be triggered first, and then the click event will be triggered twice. This will not achieve the effect of prohibiting click events.
Next, we need to prohibit the triggering of the click event in the processing logic function of the double-click event. The specific operation is to use the setTimeout() function to allow the click event to be executed after a certain period of time. If the user clicks again within this period, the last setTimeout() function is cleared and a new setTimeout() function is reset. In this way, the impact of the click event can be cleared, and only the impact of the double-click event will be retained.
The specific implementation code is as follows:
var timer = null; $("#element").bind({ click: function() { var that = this; clearTimeout(timer); timer = setTimeout(function() { //单击事件的处理逻辑 }, 200); }, dblclick: function() { clearTimeout(timer); //双击事件的处理逻辑 } });
In the above code, we define a timer variable to store the return value of the setTimeout() function so that it can be cleared later. In the click event processing logic function, first clear the last setTimeout() function, and then set a new setTimeout() function to delay the execution of the click event by 200 milliseconds. In the double-click event processing logic function, clear the last setTimeout() function, and then execute the double-click event. This way the click event is disabled.
It should be noted that the time interval of the setTimeout() function needs to be adjusted according to the actual situation. It is best to set it after testing.
To summarize, we can use the following method to prohibit click events:
The above is how to implement a double-click event to prevent the occurrence of a click event through jquery. I hope it will be helpful to you.
The above is the detailed content of How to make only double click event happen in jquery. For more information, please follow other related articles on the PHP Chinese website!