Javascript 如何以十六进制获取元素的背景颜色代码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5999209/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the background color code of an element in hex?
提问by laukok
How do I get the background color code of an element?
如何获取元素的背景颜色代码?
console.log($(".div").css("background-color"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="div" style="background-color: #f5b405"></div>
What I want
我想要的是
#f5b405
回答by Hussein
Check example link below and click on the div to get the color value in hex.
检查下面的示例链接,然后单击 div 以获取十六进制的颜色值。
var color = '';
$('div').click(function() {
var x = $(this).css('backgroundColor');
hexc(x);
console.log(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('');
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class='div' style='background-color: #f5b405'>Click me!</div>
Check working example at http://jsfiddle.net/DCaQb/
在http://jsfiddle.net/DCaQb/检查工作示例
回答by Nathan Ryan
There's a bit of a hack for this, since the HTML5 canvas is required to parse color values when certain properties like strokeStyle
and fillStyle
are set:
对此有一点小技巧,因为在设置了某些属性(如strokeStyle
和 )时,需要 HTML5 画布来解析颜色值fillStyle
:
var ctx = document.createElement('canvas').getContext('2d');
ctx.strokeStyle = 'rgb(64, 128, 192)';
var hexColor = ctx.strokeStyle;
回答by coderz
function getBackgroundColor($dom) {
var bgColor = "";
while ($dom[0].tagName.toLowerCase() != "html") {
bgColor = $dom.css("background-color");
if (bgColor != "rgba(0, 0, 0, 0)" && bgColor != "transparent") {
break;
}
$dom = $dom.parent();
}
return bgColor;
}
working properly under Chrome and Firefox
在 Chrome 和 Firefox 下正常工作
回答by coderz
In fact, if there is no definition of background-color
under some element, Chrome will output its background-color
as rgba(0, 0, 0, 0)
, while Firefox outputs is transparent
.
实际上,如果background-color
某个元素下没有定义,Chrome 会输出它的background-color
as rgba(0, 0, 0, 0)
,而 Firefox 输出的是transparent
.
回答by generalhenry
You have the color you just need to convert it into the format you want.
您拥有将其转换为所需格式所需的颜色。
Here's a script that should do the trick: http://www.phpied.com/rgb-color-parser-in-javascript/
这是一个应该可以解决问题的脚本:http: //www.phpied.com/rgb-color-parser-in-javascript/
回答by Newred
My beautiful non-standard solution
我美丽的非标解决方案
HTML
HTML
<div style="background-color:#f5b405"></div>
jQuery
jQuery
$(this).attr("style").replace("background-color:", "");
Result
结果
#f5b405
回答by Radu Di??
Adding on @Newred solution.
If your style has more than just the background-color
you can use this:
添加@Newred 解决方案。如果你的风格不仅仅是background-color
你可以使用这个:
$(this).attr('style').split(';').filter(item => item.startsWith('background-color'))[0].split(":")[1]
回答by Case
This Solution utilizes part of what @Newred and @Radu Di?? said. But will work in less standard cases.
这个解决方案利用了@Newred 和@Radu Di 的一部分??说过。但将在不太标准的情况下工作。
$(this).attr('style').split(';').filter(item => item.startsWith('background-color'))[0].split(":")[1].replace(/\s/g, '');
The issue both of them have is that neither check for a space between background-color: and the color.
他们都有的问题是都没有检查 background-color: 和颜色之间的空间。
All of these will match with the above code.
所有这些都将与上面的代码匹配。
background-color: #ffffff
background-color: #fffff;
background-color:#fffff;