First, there is a <textarea id = "test"> </textarea>
We bind the following events to it:
Copy codeThe Code is as follows: test. onkeydown = function (){
Return false;
}
Test. onkeyup = function (){
Return false;
}
Test. onkeypress = function (){
Return false;
}
We comment out two events respectively, and only one event is bound to each test.
Obviously, each function returns false. If the returned value can block the default action of the event, no content can be entered in the text box.
Let's take a look at my test results. Pay attention to the red part.
Finally, I bind the event twice, and return false each time to check whether the default action can be blocked.
Or use a flag a to test whether the onclick function returns false.
| Whether to block the default action of an event when a listener returns false. |
| |
Chrome |
IE-8 |
Firfox |
Bytes |
Safari |
| Onkeydown |
Yes |
Yes |
Yes |
No |
Yes |
| Onkeyup |
No |
No |
No |
No |
No |
| Onkeypress |
Yes |
Yes |
Yes |
Yes |
Yes |
| Onclick |
Yes |
Yes |
Yes |
Yes |
Yes |
| Keydown * 2 |
No |
Obtain the final FN result |
No |
No |
No |
| Keypress * 2 |
No |
Obtain the final FN result |
No |
No |
No |
| Click * 2 |
No |
Obtain the final FN result |
No |
No |
No |
| E. preventDefault (); |
Yes |
No |
Yes |
Yes (keydown: no) |
Yes |
| E. returnValue = false |
No |
Yes |
No |
No |
No |
It can be seen that the performance of the browser is indeed not the same, of course, IE is the most troublesome thing.
The most surprising thing is that, if you bind down in the actions, false is returned, and the default action cannot be blocked.
Therefore, it is better to use the standard method when writing to prevent the default action of the browser. (Provided later)
Otherwise, it will be quite troublesome for collaborative work.
The demo deom can send an email to me if necessary. I won't post it.Copy codeThe Code is as follows:/** use the following code to avoid tragedy.
* Final conclusion
* E (e). stop (); prevent Time bubbles
* E (e). prevent (); blocks Default Time Behavior
*/
Var E = function (e ){
E = window. event | e;
Return {
Stop: function (){
If (e & e. stopPropagation) e. stopPropagation ();
Else e. cancelBubble = true
},
Prevent: function (){
If (e & e. preventDefault) e. preventDefault ();
Else e. returnValue = false
}
}
}