jquery css颜色值返回RGB?

FFi*_*ish 16 css rgb jquery hex

在我的CSS文件中:

a, a:link, a:visited { color:#4188FB; }
a:active, a:focus, a:hover { color:#FFCC00; }
Run Code Online (Sandbox Code Playgroud)

我尝试过:

var link_col = $("a:link").css("color");
alert(link_col); // returns rgb(65, 136, 251)
Run Code Online (Sandbox Code Playgroud)

我怎样才能获得十六进制代码?

***编辑:在这里找到答案:
背景颜色十六进制到JavaScript变量

对我感到羞耻,在发布之前可能会有更好的搜索.

Ped*_*niz 8

一些调整功能

$.fn.getHexBackgroundColor = function() {
    var rgb = $(this).css('background-color');
    if (!rgb) {
        return '#FFFFFF'; //default color
    }
    var hex_rgb = rgb.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/); 
    function hex(x) {return ("0" + parseInt(x).toString(16)).slice(-2);}
    if (hex_rgb) {
        return "#" + hex(hex_rgb[1]) + hex(hex_rgb[2]) + hex(hex_rgb[3]);
    } else {
        return rgb; //ie8 returns background-color in hex format then it will make                 compatible, you can improve it checking if format is in hexadecimal
    }
}
Run Code Online (Sandbox Code Playgroud)


The*_*ist 4

在这里,这将允许您使用 $(selector).getHexBackgroundColor() 轻松返回十六进制值:

$.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(16)).slice(-2);}
    return "#" + hex(rgb[1]) + hex(rgb[2]) + hex(rgb[3]);
}
Run Code Online (Sandbox Code Playgroud)