Using jquery to get the value of the Background-color in a style, it is found that the color value obtained in the IE10 is displayed in hex format in the following version, while IE10, Chrome, and Firefox display RGB in GRB format ( 255,0,0) ", because of the need to judge the color value processing, so need to get a uniform color format, preferably hex format, easy to handle points. Search for a bit, from a foreign website to get a section of code:
Copy Code code as follows:
$.fn.gethexbackgroundcolor = function () {
var RGB = $ (this). CSS (' Background-color ');
RGB = Rgb.match (/^rgb\ (\d+), \s* (\d+), \s* (\d+) \) $/);
function Hex (x) {return ("0" + parseint (x). toString). Slice (-2);}
return rgb= "#" + Hex (rgb[1]) + hex (rgb[2]) + hex (rgb[3));
}
The above definition is a jquery function that we can pass $ ("#bg"). Gethexbackgroundcolor (); Gets the RGB value of the Background-color of the label id= "BG".
To do a little bit of modification, is to add a judgment, if it is displayed hex value (IE10 below) directly to get the value, if the non-IE browser will convert the value to RGB format:
Copy Code code as follows:
$.fn.getbackgroundcolor = function () {
var RGB = $ (this). CSS (' Background-color ');
if (RGB >= 0) return rgb;//if it is a hex value, it returns directly
else{
RGB = Rgb.match (/^rgb\ (\d+), \s* (\d+), \s* (\d+) \) $/);
function Hex (x) {return ("0" + parseint (x). toString). Slice (-2);}
rgb= "#" + Hex (rgb[1]) + hex (rgb[2]) + hex (rgb[3));
}
return RGB;
}