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