以编程方式更改按钮颜色

时间:2009-11-30 13:39:30

标签: javascript button label dashboard

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

更改标签本身
document.getElementById("button").object.textElement.innerText = "newlabel";

但是如何改变颜色?

7 个答案:

答案 0 :(得分:24)

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

document.getElementById("button").style.background='#000000';

答案 1 :(得分: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";

答案 2 :(得分:5)

可能最好更改className:

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

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

答案 3 :(得分:1)

use jquery :  $("#id").css("background","red");

答案 4 :(得分:0)

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

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

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

Background Color

Background Color Changer

答案 5 :(得分:0)

Try this code 你可能想要这样的东西     

<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";
};

答案 6 :(得分:0)

如果将其分配给一个类,它将可以正常工作:

<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>