jquery Gets the CSS color value returned RGB

Source: Internet
Author: User

The CSS code is as follows:

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

The JS code is as follows:

1 2 varlink_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 function below to turn RGB into "#xxxx" (HEX) format.

1 2 3 4 5 6 7 8 9 Ten One A - var rgbString = "rgb(0, 70, 255)"// get this in whatever way. var parts = rgbString        .match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);// parts now should be ["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.

1 2 3 4 5 6 7 function rgb2hex(rgb) { rgb = rgb.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/); function hex(x) {  return ("0" + parseInt(x).toString(16)).slice(-2); } return "#" + hex(rgb[1]) + hex(rgb[2]) + hex(rgb[3]);}

Transferred from: http://www.ghugo.com/jquery-css-color-value-returns-rgb/

jquery Gets the CSS color value returned 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.