1. <!DOCTYPE html>
    2. <html>
    3. <head>
    4. <meta charset="UTF-8">
    5. <title></title>
    6. </head>
    7. <style>
    8. .first{
    9. color: red;
    10. }
    11. .change{
    12. width: 100px;
    13. height: 60px;
    14. line-height: 60px;
    15. background-color: pink;
    16. margin: 0 auto;
    17. text-align: center;
    18. }
    19. </style>
    20. <body>
    21. <div class="first">HelloWorld</div>
    22. <script>
    23. /*
    24. * 使用element.style获得修改元素样式,如果样式较少或者功能简单的情况下使用.
    25. * 可以使用className方法,鼠标一点击,就给div加上类名
    26. * className会覆盖原来的类名
    27. * 如果想要保留原来的类名,可以加个空格
    28. */
    29. var div=document.querySelector("div");
    30. div.onclick=function(){
    31. div.className="first change";
    32. }
    33. </script>
    34. </body>
    35. </html>