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