Javascript 如何更改背景色?
我想使用javascript更改背景色。我想在单击按钮时将背景色更改为两种不同的颜色。这意味着当我单击按钮时,背景色将更改为红色,当我再次单击按钮时,背景色将更改为除绿色以外的任何其他颜色。lt意味着单击按钮后,背景色将更改为红色绿色 感谢并问候以下是代码:-Javascript 如何更改背景色?,javascript,Javascript,我想使用javascript更改背景色。我想在单击按钮时将背景色更改为两种不同的颜色。这意味着当我单击按钮时,背景色将更改为红色,当我再次单击按钮时,背景色将更改为除绿色以外的任何其他颜色。lt意味着单击按钮后,背景色将更改为红色绿色 感谢并问候以下是代码:- <button onclick = "changeBg()">Change Background colour</button> <script> function changeB
<button onclick = "changeBg()">Change Background colour</button>
<script>
function changeBg() {
if (document.body.style.backgroundColor == "red") {
document.body.style.backgroundColor = "green";
}
else if (document.body.style.backgroundColor != "green") {
document.body.style.backgroundColor = "red";
}
}
</script>
更改背景颜色
函数changeBg(){
如果(document.body.style.backgroundColor==“红色”){
document.body.style.backgroundColor=“绿色”;
}
else if(document.body.style.backgroundColor!=“绿色”){
document.body.style.backgroundColor=“红色”;
}
}
您可以运行与onclick属性相关的函数,并请求所需的样式
HTML文件
<button onclick="colorBg()">Set background</button>
如果不想在主体上应用它,可以在相关的dom上进行。在询问之前提供更多细节是很有用的。,所以你应该这样做。使用您首选的搜索提供商进行快速搜索就足以找到问题的解决方案
function colorBg() {
if (document.body.style.backgroundColor == "red") {
document.body.style.backgroundColor = "green";
}
else{
document.body.style.backgroundColor = "red";
}
}