To get the background color and font color of the web page, the method is as follows:
Thought: What we get by getting the color attribute value is RGB color, which is not what we want, so we need to change the RGB color to hexadecimal color , first get the rgb color:
var rgb = document.getElementById ('color').style.backgroundColor;
The format is as follows: rgb(225, 22, 23); Then split:
var rgb = rgb.split('(')[1]; //After splitting, it is [rgb, 225 ,22,23)] form, an array of length 2
and then split the (225,22,23) string (note: only number type can be converted, so use parseInt to force Conversion type! ):
for(var k = 0; k < 3; k ){
str[k] = parseInt(rgb .split(',')[k]).toString(16);//str array saves the split data
}
Final combination:
str = '#' str[0] str[1] str[2];
The entire code is as follows: