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