Common javascript compatible tool function Encapsulation
/**
* Created by Administrator on 2014/7/24.
*/
// I personally summarized the javascript tool functions. I hope you can help me with other functions, reply to me, and learn together.
// Add events across browsers
Function addEvent (obj, type, fn ){
If (obj. addEventListener ){
Obj. addEventListener (type, fn, false );
}
Else if (obj. attachEvent ){
Obj. attachEvent ("on" + type, fn)
}
}
// Cross-browser removal event
Function removeEvent (obj, type, fn ){
If (obj. removeEventListener ){
Obj. removeEventListener (type, fn, false );
}
Else {
Obj. detachEvent ("on" + type, fn );
}
}
// Obtain the event target object across browsers
Function getTarget (evt ){
If(evt.tar get ){
Return evt.tar get;
}
Else if (window. event. srcElement ){
Return window. event. srcElement;
}
}
// Cross-browser View
Function getInner (){
If (typeof window. innerWidth! = "Undefined "){
Return {
Width: window. innerWidth,
Height: window. innerHeight
}
}
Else {
Return {
Width: document.doc umentElement. clientWidth,
Height: document.doc umentElement. clientHeight
}
}
}
// Obtain the style from a different browser
Function getStyle (element, attr ){
If (typeof window. getComputedStyle! = "Undefined") {// W3C
Return window. getComputedStyle (element, null) [attr]
}
Else if (typeof element. currentStyle! = "Undefined") {// IE
Return element. currentStyle [attr];
}
}
// Determine whether the class exists
Function hasClass (element, className ){
Return element. className. match (new RegExp ("(\ s | ^)" + className + "(\ s | $ )"))
}
// Add link rules across browsers
Function insertRule (sheet, selectorText, cssText, position ){
If (typeof sheet. insertRule! = "Undefined") {// W3C
Sheet. insertRule (selectorText + "{" + cssText + "}", position );
} Else if (typeof sheet. addRule! = "Undefined") {// IE
Sheet. addRule (selectorText, cssText, position)
}
}
// Remove link rules from different browsers
Function deleteRule (sheet, index ){
If (typeof sheet. deleteRule! = "Undefined") {// W3C
Sheet. deleteRule (index );
}
Else if (typeof sheet. removeRule! = "Undefined") {// IE
Sheet. removeRule (index );
}
}
// Obtain the Event object
Function getEvent (event ){
Return event | window. event;
}
// Block default actions
Function preDef (event ){
Var e = getEvent (event );
If (typeof e. preventDefault! = "Undefined") {// w3c
E. preventDefault ();
} Else {// IE
E. returnValue = false;
}
}