更改P元素的样式:
<html> <body> <p id="p2">Hello World!> <script> document.getElementById("p2").style.color = "blue"; </script> <p>The paragraph above was changed by a script.</p> </body> </html>
单击按钮时,此示例更改id="id1"元素的样式:
<!DOCTYPE html> <html> <body> <h1 id="id1">My Heading 1</h1> <button type="button" onclick="document.getElementById('id1').style.color = 'red'"> 点击我!> </body> </html>