JavaScript gets the background color and font color of the Web page

Source: Internet
Author: User
Tags split
Get the background color and font color of the Web page, as follows:    thought: By obtaining the color property is worth to the RGB color, is not what we want, so you need to change the RGB color to hexadecimal color, first get RGB color:  copy code code as follows: Var RG b = document.getElementById (' color ') .style.backgroundcolor;    get format as follows: RGB (225, 22, 23); Then split the:  code as follows: var RGB = Rgb.split (' (') [1];//[RGB, 225,22,23)] in the form of a 2-length array     The (225,22,23) string is split (note : Only the number type can be converted, so use parseint to cast the type! The:  code is as follows: for (var k = 0; k < 3; k++) {  str[k] = parseint (RGB. Split (', ') [K]). toString;//str array save split data &n Bsp }    Final combination:  code is as follows: str = ' # ' +str[0]+str[1]+str[2];    The entire code follows:  code 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; }  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 (r Gb[1].split (', ') [K]). toString; }  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>&nbsp

 

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.