JavaScript如何获取十六进制颜色值而不是RGB值?
使用以下jQuery将获取元素背景色的RGB值:
$('#selector').css('backgroundColor');
有没有办法获取十六进制值而不是RGB?
回答:
var hexDigits = new Array ("0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f");
//Function to convert rgb color to hex format
function rgb2hex(rgb) {
rgb = rgb.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
return "#" + hex(rgb[1]) + hex(rgb[2]) + hex(rgb[3]);
}
function hex(x) {
return isNaN(x) ? "00" : hexDigits[(x - x % 16) / 16] + hexDigits[x % 16];
}
以上是 JavaScript如何获取十六进制颜色值而不是RGB值? 的全部内容, 来源链接: utcz.com/qa/430828.html