How to get and modify the background color and font color of a webpage in js _ javascript skills

Source: Internet
Author: User
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
 

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.