以编程方式更改按钮颜色

Zso*_*olt 24 javascript label dashboard button

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

document.getElementById("button").object.textElement.innerText = "newlabel";
Run Code Online (Sandbox Code Playgroud)

但是如何改变颜色呢?

Mau*_*bon 26

我终于找到了一个有效的代码 - 试试这个:

document.getElementById("button").style.background='#000000';
Run Code Online (Sandbox Code Playgroud)

  • document.getElementById("submit_button").style.backgroundColor ='lightgreen'; //这可以保持悬停颜色的变化 (2认同)

Nat*_*pos 6

以下是使用HTML的示例:

<input type="button" value="click me" onclick="this.style.color='#000000';
this.style.backgroundColor = '#ffffff'" />
Run Code Online (Sandbox Code Playgroud)

以下是使用JavaScript的示例:

document.getElementById("button").bgcolor="#Insert Color Here";
Run Code Online (Sandbox Code Playgroud)


mat*_*pol 5

可能最好更改className:

document.getElementById("button").className = 'button_color';
Run Code Online (Sandbox Code Playgroud)

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