36

有没有办法以编程方式改变按钮的颜色,或者至少改变按钮标签的颜色?我可以更改标签本身

document.getElementById("button").object.textElement.innerText = "newlabel";

但是如何改变颜色呢?

4

7 回答 7

41

我终于找到了一个工作代码 - 试试这个:

document.getElementById("button").style.background='#000000';
于 2010-11-13T09:11:57.133 回答
6

这是一个使用 HTML 的示例:

<input type="button" value="click me" onclick="this.style.color='#000000';
this.style.backgroundColor = '#ffffff'" />

这是一个使用 JavaScript 的示例:

document.getElementById("button").bgcolor="#Insert Color Here";
于 2009-11-30T13:49:42.773 回答
6

可能最好更改类名:

document.getElementById("button").className = 'button_color';

然后在 CSS 中添加一个按钮样式,您可以在其中设置背景颜色和其他任何内容。

于 2009-11-30T13:51:17.990 回答
1
use jquery :  $("#id").css("background","red");
于 2014-09-01T09:55:25.497 回答
1

如果将它分配给一个类,它应该可以工作:

<script>
  function changeClass(){
    document.getElementById('myButton').className = 'formatForButton';
  }
</script>

<style>
  .formatForButton {
    background-color:pink;
   }
</style>

<body>
  <input id='myButton' type=button class=none value='Change Color to pink' onclick='changeClass()'>
</body>
于 2018-08-24T18:41:33.147 回答
0

我相信你想要bgcolor。像这样的东西:

document.getElementById("button").bgcolor="#ffffff";

以下是一些可能会有所帮助的演示:

背景颜色

背景换色器

于 2009-11-30T13:51:04.807 回答
0

试试这个代码 你可能想要这样的东西

<button class="normal" id="myButton" 
        value="Hover" onmouseover="mouseOver()" 
        onmouseout="mouseOut()">Some text</button>

然后在你的 .js 文件上输入这个。确保你的 html 连接到你的 .js

var tag=document.getElementById("myButton");

function mouseOver() {
    tag.style.background="yellow";
};
function mouseOut() {
    tag.style.background="white";
};
于 2018-06-09T18:14:30.493 回答