jquery get CSS color value return RGB application

Source: Internet
Author: User

The code is as follows:

The code is as follows Copy Code

A, a:link, a:visited {color: #4188FB;}
A:active, A:focus, a:hover {color: #FFCC00;}

The JS code is as follows:

The code is as follows Copy Code

var Link_col = $ ("A:link"). CSS ("color");
alert (Link_col); Returns RGB (65, 136, 251)

Jquey seems to set the color, using the RGB format.

Use the following function to convert RGB to the #xxxx (HEX) format.

The code is as follows Copy Code

var rgbstring = "RGB (0, 70, 255)"; Get this in whatever way.
var parts = rgbstring
. Match (/^rgb (d+), s* (d+), s* (d+)) $/)
;
Parts now should is ["RGB (0, 70, 255", "0", "70", "255"]
Delete (parts[0]);
for (var i = 1; I <= 3; ++i) {
Parts[i] = parseint (Parts[i]). toString (16);
if (parts[i].length = = 1) parts[i] = ' 0 ' + parts[i];
}
var hexstring = Parts.join ('); "0070FF"

or use this function.

The code is as follows Copy Code

function Rgb2hex (RGB) {
RGB = Rgb.match (/^rgb (d+), s* (d+), s* (d+)) $/);
function Hex (x) {
Return ("0" + parseint (x). toString). Slice (-2);
}
Return "#" + Hex (rgb[1]) + hex (rgb[2]) + hex (rgb[3));
}

Add

Get a problem with the color values in a CSS style, different browser formats for different solutions

Using jquery to get the value of the Background-color in a style, it is found that the color value obtained in the IE10 is displayed in hex format in the following version, while IE10, Chrome, and Firefox display RGB in GRB format ( 255,0,0) ", because of the need to judge the color value processing, so need to get a uniform color format, preferably hex format, easy to handle points. Search for a bit, from a foreign website to get a section of code:

The code is as follows Copy Code

$.fn.gethexbackgroundcolor = function () {
var RGB = $ (this). CSS (' Background-color ');
RGB = Rgb.match (/^rgb (d+), s* (d+), s* (d+)) $/);
function Hex (x) {return ("0" + parseint (x). toString). Slice (-2);}
return rgb= "#" + Hex (rgb[1]) + hex (rgb[2]) + hex (rgb[3));
}

The above definition is a jquery function that we can pass $ ("#bg"). Gethexbackgroundcolor (); Gets the RGB value of the Background-color of the label id= "BG".

To do a little bit of modification, is to add a judgment, if it is displayed hex value (IE10 below) directly to get the value, if the non-IE browser will convert the value to RGB format:

  code is as follows copy code


$.fn.getbackgroundcolor = function () {
     var RGB = $ (this). CSS (' Background-color ');
     if (RGB >= 0) return rgb;//if it is a hex value then go straight back
    else{
          RGB = Rgb.match (/^rgb (d+), s* (d+), s* (d+)) $/);
         function Hex (x) {return ("0" + parseint (x). toString). Slice ( -2);}
         rgb= "#" + Hex (rgb[1]) + hex (rgb[2]) + hex (rgb[3));
    }
     return RGB;
 }

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.