Common javascript compatible tool function Encapsulation

Source: Internet
Author: User

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;
}
}

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.