YAHOO. util. Dom-Part.3

Source: Internet
Author: User

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.

Related Article

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.