How to obtain the webpage background color and font color in js

Source: Internet
Author: User

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:
Copy codeThe Code is as follows:
Var rgb = document. getElementById ('color'). style. backgroundColor;

The format is as follows: rgb (225, 22, 23); then split:
Copy codeThe 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 !) :
Copy codeThe Code is as follows:
For (var k = 0; k <3; k ++ ){
Str [k] = parseInt (rgb. split (',') [k]). toString (16); // The str array stores the split data.
}

Final combination:
Copy codeThe Code is as follows:
Str = '#' + str [0] + str [1] + str [2];

The Code is as follows:
Copy codeThe Code is as follows:
<! DOCTYPE html>
<Html>
<Head>
<Title> getHexColor js/jQuery get hexadecimal color </title>
<Meta charset = "UTF-8"/>
<Script type = "text/javascript">
Function getHexBgColor (){
Var str = [];
Var rgb = document. getElementById ('color'). style. backgroundColor. split ('(');
For (var k = 0; k <3; k ++ ){
Str [k] = parseInt (rgb [1]. split (',') [k]). toString (16 );
}
Str = '#' + str [0] + str [1] + str [2];
Document. getElementById ('color'). innerHTML = str;
}
Function getHexColor (){
Var str = [];
Var rgb = document. getElementById ('color'). style. color. split ('(');
For (var k = 0; k <3; k ++ ){
Str [k] = parseInt (rgb [1]. split (',') [k]). toString (16 );
}
Str = '#' + str [0] + str [1] + str [2];
Document. getElementById ('color'). innerHTML = str;
}
</Script>
<Style type = "text/css">
# Color {
Width: 200px;
Height: 200px;
Line-height: 200px;
Text-align: center;
}
</Style>
</Head>
<Body>
<Div style = "color: #88ee22; background-color: # ef8989;" id = "color"> </div>
<Input onclick = "getHexBgColor ();" type = "button" value = "get background color"/>
<Input onclick = "getHexColor ();" type = "button" value = "Get font color"/>
</Body>
</Html>

Related Article

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.