运行 ❯
获取您
自己的
网站
×
更改方向
保存代码
更改主题,深色/浅色
转到 Spaces
<!DOCTYPE html> <html> <style> .myStyle { background-color: coral; padding: 16px; } .newStyle { background-color: lightblue; text-align: center; font-size: 25px; padding: 16px; } </style> <body> <h1>The Element Object</h1> <h2>The className Property</h2> <button onclick="myFunction()">Toggle</button> <p>Click "Toggle" (many times) to toggle between two classes.</p> <div id="myDIV" class="myStyle"> <p>I am myDIV.</p> </div> <script> function myFunction() { const element = document.getElementById("myDIV"); if (element.className == "myStyle") { element.className = "newStyle"; } else { element.className = "myStyle"; } } </script> </body> </html>