获取#000格式而不是RGB的背景色 [英] Get background-color in #000 format and not RGB

查看:107
本文介绍了获取#000格式而不是RGB的背景色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用此代码,我得到表中任何TD的RGB颜色:

With this code, I get the RGB color of any TD in my table :

alert($(this).css('background-color'));

结果是:

rgb(0,255,0)

rgb(0, 255, 0)

jquery是否可以获取#000格式,或者我可以使用函数将rgb转换为#000格式?

Is it possible with jquery to obtain the #000 format or have I to use a function to transform the rgb in #000 format ?

预先感谢您的帮助

推荐答案

尝试

var color = '';
$('div').click(function() {
   var hexcolor = $(this).css('backgroundColor');
   hexc(hexcolor);
   alert(color);
});

function hexc(colorval) {
    var parts = colorval.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
    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];
    }
    color = '#' + parts.join('');

    return color;
}

这篇关于获取#000格式而不是RGB的背景色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆