Situation description
When an element, such as a Div, binds both the Click event and the DblClick event, and the two events are handled in a relatively independent business, that is, the click cannot trigger the Dblclick,dblclick when it is not triggered. In the actual test found that when the DblClick, there will always be 1 times click. This is the problem that will be addressed below.
Situation Analysis
First we need to be clear about the order of execution of Click and DblClick, the test procedure is slightly, the following is the test result:
Click:mousedown--MouseUp--click
Dblclick:mousedown--MouseUp--click--MouseDown--MouseUp--click--DblClick
In this view, before the DblClick trigger, actually executes 2 times click, and the first click will be blocked (why?) Well, I don't know either.
Solution
The first thought is whether you can stop the event, but find that the browser does not provide the appropriate method, if it is too difficult to implement, because click the event associated with the behavior must be made can be cancel the line.
So consider the delay, is the only solution I can think of, using settimeout () to delay the completion of the Click event, and then use Cleartimeout () to stop when you need to screen click.
Specific code
Copy Code code as follows:
var test = (function () {
var clicktext = ' click<br> ';
var dblclicktext = ' dblclick<br> ';
var timer = null;
return {
Click:function () {
Cleartimeout (timer);
Timer = settimeout (function () {
$ (' body '). Append (Clicktext);
}, 300);
},
Dblclick:function () {
Cleartimeout (timer);
$ (' body '). Append (Dblclicktext);
},
Init:function () {
$ (function () {
$ (' div '). Click (Test.click). DblClick (Test.dblclick);
});
}
}
})();
Test.init ();
HTML code
Copy Code code as follows:
<div style= "Width:100px;height:100px;background:red;text-align:center;line-height:33px;-moz-user-select:none ;-khtml-user-select:none;user-select:none ">click<br>or<br>dblclick</div>
Subsequent
The article title said, is not perfect, because Windows, Control Panel can be adjusted mouse double-click speed (other systems are not clear), so I set the system set the double-click speed is slow, then the above demo will not take effect. So 300 milliseconds is just a ballpark.
Author: Hu Yuan