This article describes how to obtain and modify the background color and font color of a webpage in JavaScript, and describes how to perform operations on page element attributes in JavaScript, for more information about how to obtain and modify the webpage background color and font color in js, see the following example. We will share this with you for your reference. The details are as follows:
To obtain the background color and font color of a webpage, follow these steps:
Idea: The rgb color is obtained by obtaining the color attribute value, which is not what we want. Therefore, we need to replace the rgb color with the hexadecimal color to first obtain the rgb color:
The code is as follows:
The code is as follows:
Var rgb = document. getElementById ('color'). style. backgroundColor;
The format is as follows: rgb (225, 22, 23); then split:
The code is as follows:
The code is as follows:
Var rgb = rgb. split (') [1]; // an array of 2 in the format of [rgb, 225,22, 23)] after splitting
Then split the string (, 22, 23) (Note: only the number type can be converted, so use parseInt to forcibly convert the type !) :
The code is as follows:
For (var k = 0; k <3; k ++) {str [k] = parseInt (rgb. split (',') [k]). toString (16); // str array saves the split data}
Final combination:
The code is as follows:
The code is as follows:
Str = '#' + str [0] + str [1] + str [2];
The complete code is as follows:
GetHexColor js/jQuery get hexadecimal color