JS Blocks Browser default behavior and stops event bubbling __js

Source: Internet
Author: User

Stop event bubbling and "block browser default behavior" are frequently used in front-end development work due to browser compatibility issues.

1. Block browser default behavior [Java]   View plain copy Function stopdefault (e)  {            //If an event object is provided, this is a non-ie browser              if (e && e.preventdefault)  {             //block default browser actions (web)             E.preventdefault ();           } else {            how to block the default action of a function in//ie               window.event.returnvalue = false;             }           return false;        }  


2. Stop event bubbling [Java] view plain copy function stopbubble (e) {//If an event object is provided, this is a non-IE browser if (e && e.s        Toppropagation) {//Therefore it supports the Stoppropagation () method E.stoppropagation () of the consortium;       else {//otherwise, we need to use IE to cancel event bubbling window.event.cancelBubble = true;    return false; }


Concrete Application Example: Write a good project, today to the user to use, returned a lot of problems, which have a very fine canon:

A page, there is a form, the button used to submit the form is a buttons, with jquery to respond to the button's click Action, through post submission, for the user input is a text box, the user input to fill in after the item, directly press ENTER, it is equivalent to press the button, At the beginning did not pay attention to this problem, a press RETURN, jump to another page, a lot of data, just found to block the browser's default behavior, because the default behavior is submit form, then your JS will not be implemented. So cancel the default behavior first. Then execute your JQ to submit. I can't say for sure, only know if the text box type= "Submit", the direct click of the button will jump to another page, if type= "button", then click the button will not appear in this case, you can press ENTER when you will jump to another page, the solution, Look at the following code:

JSP code: [Java] view plain copy <input type= "text" name= "appgrpname_s" id= "appgrpname_s" onkeydown= "Enter_" Down (this.form, event); />

JS Code:[Java]  View plain copy <script type= "Text/javascript" >       function  enter_down (form, event)  {          if ( event.keycode==  "a")  {              Stopdefault (event);             submitform (form, ' Actiondiv ');         }       }              function stopdefault (e)  {           //If an event object is provided, this is a non-ie browser              if (e && e.preventdefault)  {             //block default browser actions (web)             E.preventdefault ();            } else {              how to block the default action of a function in//ie              window.event.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.