JS prevents browser default behavior and stops event bubbling (enter with jquery for carriage return submission, compatible with IE, FF browser) __JS

Source: Internet
Author: User
Tags tagname

In the front-end development work, because of browser compatibility issues, we often use "stop event bubbling" and "block browser default behavior."

1. Block Browser default behavior

function Stopdefault (e) {
		//If an event object is provided, this is a non-ie browser if 
		(e && e.preventdefault) {
		//block default browser action (web)
		E.preventdefault ();
		} How else {
		//ie prevents the default action of the function in 
		Window.event.returnValue = false; 
		}
		return false;
	}


2. Stop event bubbling

function Stopbubble (e) {
    //If an event object is provided, this is a non-ie browser if
    (e && e.stoppropagation) {
  //  It therefore supports the stoppropagation () method of the
  e.stoppropagation (); 
    } 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:

<input type= "text" name= "appgrpname_s" id= "appgrpname_s" onkeydown= "Enter_down (This.form, event);" />

JS Code:

<script type= "Text/javascript" >
    function Enter_down (form, event) { 
      if (event.keycode== ")" {
	      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 action (web) c10/> E.preventdefault ();
		} How else {
		//ie prevents the default action of the function in 
		Window.event.returnValue = false; 
		}
		return false;
	}
</script>

By using the above code, you can click the "Submit" button when you press ENTER. And the above code compatible with IE, FF browser.

Sometimes encountered need to screen some of the browser shortcut key behavior, such as: FF press backspace key, will jump to the previous browser history page;

Note to invoke the Stopdefault (event) function in the onkeydown event, the call in the OnKeyUp event is unsuccessful.

<a onclick= "Togglefriendfunclist (event, ' 6708062 ', ' he ');" ></a>

Because HREF is a null value, if the browser's default behavior is not blocked, the effect is to refresh the page.
What we need to do now is to block the link events of href and execute the onclick event.
Old way of handling:

<a onclick= "Togglefriendfunclist" (Event, ' 6708062 ', ' he '); "href=" javascript:void (0); " ></a>

jquery's writing:
1) Return False:in event handler, prevents default behavior and event bubbing.
return false in the handling of an event, you can block default events and bubbling events.
2) Event.preventdefault (): In event handler, prevent default event (allows bubbling).
Event.preventdefault () in the handling of events, you can block the default event but allow bubbling events to occur.
3) Event.stoppropagation (): In event handler, prevent bubbling (allows default behavior).
Event.stoppropagation () in the handling of events, you can block bubbles but allow default events to occur

Prototype's writing:
Event.stop (Event)
Usage Introduction:
After an event occurs, the browser usually triggers the event handler on the element where the event occurred first, followed by its parent element, the parent element ... And so on, until the root element of the document. This is known as event bubbling, the most common way to propagate events. When dealing with an event, you may want to stop the event from spreading and not want it to continue bubbling.
When your program has the opportunity to handle an event, if the event has a default behavior, the browser will also handle it. For example, click the navigation link, submit the form to the server, press the ENTER key in a single line of text box, and so on. If you define your own handling of these events, you may be very interested in blocking the associated default behavior.

However, sometimes still can not solve the corresponding problem, obviously has called the browser to prevent the default behavior of the method, you can press the return time or will call the default behavior, and finally did not find the problem, had to turn the key to disabled, is actually using the tab keys instead of enter. The code is as follows:

<script language= "javascript" event= "onkeydown" for= "document" >//For return enter if (Event.keycode = 13) {//Change to Tab
   Key, so that every time you press Enter the TAB function, the cursor jumps to the next object event.keycode = 9; } </script> <script language= "javascript" type= "Text/javascript" >//Disabling the Enter key form Autocommit Document.onkeyd  
        own = function (event) {var target, code, tag;  
            if (!event) {event = window.event//for IE browser target = event.srcelement;  
            Code = Event.keycode;  
                 if (code = =) {tag = Target.tagname;  
                 if (tag = = "TEXTAREA") {return true;}  
             else {return false;} } else {target = Event.target;//For browsers that follow the standards of the consortium, such as Firefox code = even  
            T.keycode;  
                 if (code = =) {tag = Target.tagname;  
                 if (tag = = "INPUT") {return false;}  else {return true;} 
            }  
       }  
     }; </script>


Specific Use sample:

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" > <%@ page language= "java" import= "java.util.*" Pageenc oding= "UTF-8"%> <%@ include file= "/pages/common/global.jsp"%>  







 

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.