有没有办法以编程方式改变按钮的颜色,或者至少改变按钮标签的颜色?我可以更改标签本身
document.getElementById("button").object.textElement.innerText = "newlabel";
但是如何改变颜色呢?
有没有办法以编程方式改变按钮的颜色,或者至少改变按钮标签的颜色?我可以更改标签本身
document.getElementById("button").object.textElement.innerText = "newlabel";
但是如何改变颜色呢?
我终于找到了一个工作代码 - 试试这个:
document.getElementById("button").style.background='#000000';
这是一个使用 HTML 的示例:
<input type="button" value="click me" onclick="this.style.color='#000000';
this.style.backgroundColor = '#ffffff'" />
这是一个使用 JavaScript 的示例:
document.getElementById("button").bgcolor="#Insert Color Here";
可能最好更改类名:
document.getElementById("button").className = 'button_color';
然后在 CSS 中添加一个按钮样式,您可以在其中设置背景颜色和其他任何内容。
use jquery : $("#id").css("background","red");
如果将它分配给一个类,它应该可以工作:
<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>
试试这个代码 你可能想要这样的东西
<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";
};