最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - How to know button's background color in javascript? - Stack Overflow

programmeradmin5浏览0评论

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
Add a ment  | 

3 Answers 3

Reset to default 5

You 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)">

发布评论

评论列表(0)

  1. 暂无评论