改变颜色的方法:使用按钮
P粉111927962
2023-09-05 16:38:19
<p>有没有办法让我在这个函数中写更少的代码行?我希望当你按下其中一个按钮时,它会在两种颜色之间切换。函数green()是red的完全相反。</p>
<pre class="lang-html prettyprint-override"><code><h1 id="header">红色还是绿色</h1>
<button id="redButton" onclick="red()">红色</button>
<button id="greenButton" onclick="green()">绿色</button>
</code></pre>
<pre class="brush:php;toolbar:false;">function red() {
document.getElementById("header").innerHTML = "红色"
document.getElementById('header').style.color = "red"
document.getElementById('redButton').style.color = "white"
document.getElementById('redButton').style.backgroundColor = "red"
document.getElementById('greenButton').style.color = "black"
document.getElementById('greenButton').style.backgroundColor = "grey"
}</pre></p>