JS bubbling event and Event monitor usage Analysis _javascript tips

Source: Internet
Author: User
Bubbling events
JS "Bubbling Event" (bubble) is not a practical use of fancy techniques, it is a JS event execution sequence mechanism, "bubble algorithm" in the programming is a classic problem, bubble algorithm inside the "bubble" should be said to exchange more accurate; JS inside the "bubble event" is the real meaning of the bubble , it traverses the tree from the lowest layer of the DOM, and then attaches the corresponding event. Take the following code as an example:
<title> bubbling event </title> <script type= "Text/javascript" > Function Add (stext) {document.getElementById ( "Console"). InnerHTML +=stext; } </script> </pead> <body onclick= "Add (' body Event Trigger ')" > <div onclick= "Add (' div event trigger ')" > <p o nclick= "Add (' P Event trigger ')" > click </p> </div> <div id= "Console" ></div> </body>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

View Run effects
Event Monitoring
The incident listens to be accurate to say is the JS engine to the user mouse, keyboard, the window event and so on action surveillance carries on the operation, namely is for the user corresponding operation to carry on the additional event, commonly used similar btnadd.onclick= "alert (' 51obj.cn ')" is simply an additional event, except that this method does not support attaching multiple events and deleting events. The following code will implement the Append event to delete the event (IE):
<script type= "Text/javascript" > <!–var OP; function Window.onload () {Op=document.getelementbyid ("pcontent"); Op.attachevent ("onclick", click); function Click () {alert ("do something"); Op.detachevent ("onclick", click); //–> </script> </pead> <body> <p id= "Pcontent" > Click </p>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

View Run Results
IE can not be called standard Dom browser, even the latest IE8, compared to the standard Dom such as Firefox, opera, etc., it is a "heterogeneous"; in Firefox, there really is a function called event listening AddEventListener, as the following example
<script type= "Text/javascript" > <!–window.onload=function () {var Obtn=document.getelementbyid ("btn"); Obtn.addeventlistener ("click", Click,false); function Click () {alert ("Trigger click event"); }//–> </script> <button id= "BTN" > Click </button>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

View Run effects
From the above two examples to see attachevent in Firefox does not support, IE also does not support AddEventListener. Therefore, you need to use a compatibility method.
<script type= "Text/javascript" > <!–var obtn; Window.onload=function () {Obtn=document.getelementbyid ("btn"); if (Window.addeventlistener) {Obtn.addeventlistener ("click", Click,false); }//ff,opera. else if (window.attachevent) {obtn.attachevent ("onclick", click,false); }//ie else{Obtn.onclick=click; }//other} function Click () {alert ("event only executes once"); if (Window.addeventlistener) {Obtn.removeeventlistener ("click", Click,false); }//ff else if (window.attachevent) {obtn.detachevent ("onclick", click); } else{Obtn.onclick=null; }//–> </script> </pead> <body> <button id= "btn" >www.jb51.net»»</button> </bo dy>
[ CTRL + A All SELECT Note: If you need to introduce external JS need to refresh to perform]

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.