JavaScript Learning--8.1

Source: Internet
Author: User

JavaScript Learning--8.1 (Common JS Code compatibility Tool Summary)

1. Take CSS style sheets and non-inline style properties

function GetStyle (obj,attr) {    if(obj.currentstyle) {        return  OBJ.CURRENTSTYLE[ATTR];    } Else  {        return window.getcomputedstyle (obj,null) [attr]; An object that needs to select a property and a pseudo-element word such as (: After)    }}

Note: The element attributes obtained by this method have units, such as height, width is px

2. To obtain elements by class

function Getbyclass (classname) {       var result=[];     // the element       used to hold the filter var elements=document.getelementsbytag ("*");         for (var i=0;i<elements.length;i++) {               if(elements[i].classname==  ClassName) {                        Result.push (elements[i]);               }       }        return result;}                

Note: The obtained is an array form, you need to select the plus subscript is good

3. Compatibility Get Event Object

function GetEvent (e) {     var oevent=e| | window.event;      return oevent;}

4. Add a multi-event handle

function addevent (element,type,handle) {        if(element.addeventlistener) {        //DOM2 level             Element.addeventlistener (Type,handler,false);    // Execute event bubbling stream        } Else if (element.attachevent) {            element.attachevent (' on ' +Type,handler);        } Else {            element[' on ' +type]=handler;    // the notation supported by any element         }}

5. Removing multi-event handles

 function   (Element,type,handler) { if  (Element.removeeventlistener) {// false ); //  Execute event bubbling stream } else  if   (element.detachevent) {element.detachevent ( ' on ' +type,handler);  else  {element[ ' on ' +type]=
   
    null ; 
    // 
      
   

6. Return the type of event

function GetType (e) {     var oevent=e| | window.event;     return Oevent.type;    // There is no compatibility issue }

7. Return the element where the event is located

function Gete (e) {    var oevent=e| | window.event;     Return oevent.target| | oevent.srcelement;}

8. Cancel Event Bubbling

function Stop (e) {     var oevent=e| | window.event;      if (oevent.stoppropagation) {            oevent.stoppropagation ();     } Else {            oevent.cancelbubble=true;  // Cancel Event Bubbling      }}

9. Canceling the default behavior

function Prevent (e) {      var oevent=e| | window.event;       if (oevent.preventdefault) {           oevent.preventdefault ();      } Else {            Oevent.returnvalue=false;  // Cancel Default Event       }}

10. Add multiple events for onload

function Addloadevent (func) {    var oldload=window.onload;     if (typeof(window.onload)! = "function") {        window.onload=func;    } Else {        window.onload=function() {            oldload ();            Func ();     }}}

JavaScript Learning--8.1

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.