改变 HTML 元素的背景色

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.  
  5. <script>
  6. function bgChange(bg) {
  7. document.body.style.background = bg;
  8. }
  9. </script>
  10. </head>
  11. <body>
  12.  
  13. <h1>改变背景颜色</h1>
  14.  
  15. <p>请把鼠标移动到方块上。</p>
  16.  
  17. <table style="width:300px;height:100px">
  18. <tr>
  19. <td onmouseover="bgChange(this.style.backgroundColor)"
  20. onmouseout="bgChange('transparent')"
  21. style="background-color:Khaki">
  22. </td>
  23. <td onmouseover="bgChange(this.style.backgroundColor)"
  24. onmouseout="bgChange('transparent')"
  25. style="background-color:PaleGreen">
  26. </td>
  27. <td onmouseover="bgChange(this.style.backgroundColor)"
  28. onmouseout="bgChange('transparent')"
  29. style="background-color:Silver">
  30. </td>
  31. </tr>
  32. </table>
  33.  
  34. </body>
  35. </html>