本文介绍了如何获取元素的背景颜色代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何获取元素的背景颜色代码?
How do I get the background-color code of an element?
HTML
<div style="background-color:#f5b405"></div>
jQuery
$(this).css("background-color");
结果
rgb(245, 180, 5)
我想要什么
#f5b405
推荐答案
查看下面的示例链接,点击div获取十六进制颜色值。
Check example link below and click on the div to get the color value in hex.
var color = '';
$('div').click(function() {
var x = $(this).css('backgroundColor');
hexc(x);
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('');
}
查看
Check working example at http://jsfiddle.net/DCaQb/
这篇关于如何获取元素的背景颜色代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!