Var patterns = {
HYPHEN:/(-[a-z])/I,
ROOT_TAG:/^ body | html $/I
};
Var toCamel = function (property ){
// If no-[a-z] letter exists, return directly
If (! Patterns. HYPHEN. test (property )){
Return property;
}
// If there is a cache, the replaced value is directly returned.
If (propertyCache [property]) {
Return propertyCache [property];
}
// Use regular expression replacement
Var converted = property;
While (patterns.HYPHEN.exe c (converted )){
Converted = converted. replace (RegExp. $1,
RegExp. $1. substr (1). toUpperCase ());
}
// Save to cache
PropertyCache [property] = converted;
Return converted;
}; In YAHOO. util. Dom, The getStyle function considers more compatibility issues with different browsers. The Code is as follows:
// Use W3C DOM standard browsers, such as Firefox, Opera, and Safari
If (document. defaultView & document. defaultView. getComputedStyle ){
GetStyle = function (el, property ){
Var value = null;
// Rename some CSS style names
If (property = 'float '){
Property = 'cssfloat ';
}
// Obtain the attributes added with CSS.
Var computed = document. defaultView. getComputedStyle (el ,'');
If (computed ){
Value = computed [toCamel (property)];
}
Return el. style [property] | value;
};
// For IE browser
} Else if (document.doc umentElement. currentStyle & isIE ){
GetStyle = function (el, property ){
Switch (toCamel (property )){
// The "Conversion" name can be recognized by IE
Case 'opacity ':
Var val = 100;
Try {
Val =
El. filters ['dximagetransform. Microsoft. alpha']. opacity;
} Catch (e ){
Try {
Val = el. filters ('alpha'). opacity;
} Catch (e ){
}
}
// Percentage
Returns val/100;
Case 'float ':
Property = 'stylefloat ';
Default:
Var value = el. currentStyle? El. currentStyle [property]: null;
Return (el. style [property] | value );
}
};
} Else {
// Obtain the inline Style
GetStyle = function (el, property) {return el. style [property] ;};
} In addition, PPK's elaborate on getStyle on his Blog is also wonderful. If you are interested, you can check it out.