【发布时间】:2021-10-27 19:29:18
【问题描述】:
我的目标是更改 onClick 的背景颜色。我已经成功地做到了,但是当颜色改变时,我用来设置按钮样式的 CSS 不再适用。单击后如何保持按钮的 CSS 样式
const colors = ["blue", "green", "yellow"];
let colorIndex = -1;
function myFunction(){
colorIndex += 1;
if (colorIndex > colors.length-1) colorIndex = 0;
document.body.style.backgroundColor = colors[colorIndex]
}
let colorSindex = -1;
function myyFunction(){
colorSindex +=1;
if (colorSindex > colors.length-1) colorSindex = 0;
document.getElementById("btn").innerHTML = colors[colorSindex]
}
.top{
display: flex;
justify-content: space-around;
border: black 1px solid;
background-color: chartreuse;
}
.main{
text-align: center;
margin-top: 15%;
border: black 1px solid;
display: inline-block;
position: relative;
left: 40%;
}
button{
cursor: pointer;
}
<div class="top">
<ul>Colour Flipper</ul>
<ul>Simple</ul>
<ul>Hex</ul>
</div>
<div id="main">
<button id="btn" onclick="myFunction();myyFunction()">
<h1>Background Colour</h1>
</button>
</div>
【问题讨论】:
标签: javascript html css