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.