Return false: Test code for the default action of the blocked event

Source: Internet
Author: User

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
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.