admin管理员组文章数量:1394135
I have a yellow button.
When I click the button, I want to get the button's color.
I tried this:
<html>
<head>
<style rel="stylesheet">
.A {
background-color: #ffff00
}
</style>
<script>
function clicked() {
console.log(document.getElementById("A1").style.backgroundColor);
}
</script>
</head>
<body>
<input type="button" id="A1" class="A" value="1" onclick="clicked()">
</body>
</html>
I have a yellow button.
When I click the button, I want to get the button's color.
I tried this:
<html>
<head>
<style rel="stylesheet">
.A {
background-color: #ffff00
}
</style>
<script>
function clicked() {
console.log(document.getElementById("A1").style.backgroundColor);
}
</script>
</head>
<body>
<input type="button" id="A1" class="A" value="1" onclick="clicked()">
</body>
</html>
then I got nothing.
How can I get 'yellow' or '#ffff00'?
Share Improve this question edited Nov 25, 2018 at 7:17 Nick Parsons 51.2k6 gold badges57 silver badges77 bronze badges asked Nov 25, 2018 at 6:57 Hoseong JeonHoseong Jeon 1,3502 gold badges14 silver badges21 bronze badges 2-
You are probably looking for
getComputedStyle
. – Alexander Nied Commented Nov 25, 2018 at 7:05 - 1 Possible duplicate of How to get `background-color` property value in Javascript? – Mohammad Commented Nov 25, 2018 at 7:57
3 Answers
Reset to default 5You can try to use getComputedStyle
method to get the button style:
function clicked() {
var button = document.getElementById("A1");
var style = getComputedStyle(button);
console.log(style['background-color']);
}
.A { background-color: #ffff00 }
<input type="button" id="A1" class="A" value="1" onclick="clicked()">
Your CSS
<style rel="stylesheet">
.A { background-color: #ffff00 }
</style>
You must write this way
<input type="button" id="A1" class="A" value="1" onclick="clicked()">
<script>
function clicked() {
const element = document.getElementById('A1');
const style = getComputedStyle(element);
console.log(style.backgroundColor);
}
</script>
You have to use window.getComputedStyle().
The
window.getComputedStyle()
method returns an object that reports the values of all CSS properties of an element after applying active stylesheets and resolving any basic putation those values may contain. Individual CSS property values are accessed through APIs provided by the object or by simply indexing with CSS property names.
Then you have to convert the rgb value to hex.
You can try the following way:
function clicked(el) {
var bg_color = window.getComputedStyle(el, null).backgroundColor;
bg_color = bg_color.match(/\d+/g);
console.log(rgbToHex(bg_color));
}
function ponentToHex(c) {
var hex = c.toString(16);
return hex.length == 1 ? "0" + hex : hex;
}
function rgbToHex(rgb) {
return "#" + ponentToHex(+rgb[0]) + ponentToHex(+rgb[1]) + ponentToHex(+rgb[2]);
}
.A { background-color: #ffff00 }
<input type="button" id="A1" class="A" value="1" onclick="clicked(this)">
本文标签: htmlHow to know button39s background color in javascriptStack Overflow
版权声明:本文标题:html - How to know button's background color in javascript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744642572a2617209.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论