Currentstyle IE and FF get external styles (from: http://hi.baidu.com/aganle/blog/item/86109bcf27d2b70b93457e41.html)

Source: Internet
Author: User

Available for IE and OPCurrentstyleFor detailed usage, see DHTML manual.
Firefox and other browsers can use the following method to obtain

Window. getcomputedstyle (element, ''). getpropertyvalue (stylevalue );

Note that both methods are read-only.

So I wrote a common function:

Function getstyle (Ele, value) {/* ----- obtain the true style of an element -----*/
VaR RS;
If (Ele. Style [value]) {
Rs = ELE. Style [value];
} Else if (window. getcomputedstyle ){
Value = value. Replace (/([A-Z])/g, "-$1 ");
Rs = Window. getcomputedstyle (Ele, ''). getpropertyvalue (value );
If (value = 'color') {rs = colortohex (RS);} // format the color in hexadecimal notation.
} Else if (Ele. currentstyle ){
Rs = ELE. currentstyle [value];
} Else {
Return NULL;
}
Return Rs;
}

However, different browsers obtain different color values (FF/SA is an RGB value, op is a hexadecimal value, and currentstyle obtains the original valid values in CSS)
Therefore, you need to convert the color value (hexadecimal representation, and the pre-name color cannot be formatted ):

Function colortohex (color) {/* ----- format the color in the style in hexadecimal format. The pre-named color cannot be formatted -----*/
VaR RGB =/RGB/(/d {1, 3}), (/s) * (/d {1, 3}), (/s) * (/d {1, 3 }) /) //; // RGB regular expression. The result ordinal number is 1, 3, and 5.
VaR noun =/^/W +/; // pre-named color regular
VaR RS;
If (RGB. Test (color )){
VaR rgbfix = rgb.exe C (color );
VaR R, G, B;
R = parseint (rgbfix [1]). tostring (16 );
G = parseint (rgbfix [3]). tostring (16 );
B = parseint (rgbfix [5]). tostring (16 );
Rs = '#' + (r <10? '0' + R: R) + (G <10? '0' + G: G) + (B <10? '0' + B: B );
} Else {
Rs = color;
}
If (Rs. Length = 4 &&! %. Test (RS) {rs = Rs + Rs. Split ('#') [1]} // corrected the hexadecimal notation and adopted the length judgment method.
Return Rs;
}

5.23
Fixed the problem where the getpropertyvalue method was used to obtain a value similar to paddingleft.
Optimization Efficiency: When the style attribute is defined, the value of the style is prioritized, and the color value is corrected only when the value of getcomputedstyle is set.

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.