_12_dom练习.html 986 B

1234567891011121314151617181920212223242526272829303132333435
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. </head>
  7. <body>
  8. <div id ="div1"></div>
  9. <img id="img" src="img/1.png" alt="">
  10. <button onclick="fun()">修改div 内容</button>
  11. <button onclick="fun1()">变成美女</button>
  12. </body>
  13. <script>
  14. /**
  15. * 需求1: 点击button 按钮. 把div 内容设置成我是中国人
  16. * 需求2: 点击变成美女按钮, 把img src 属性修改成img/2.png
  17. */
  18. function fun(){
  19. // 1 获取id 为div 元素 document.getElementById()
  20. var div1 = document.getElementById("div1")
  21. // 2 给元素去设置内容为我是中国中 元素.innertText
  22. div1.innerText = "我是中国人"
  23. }
  24. /**
  25. * dom 操作不论事操作元素本身
  26. * 还是操作元素的内容 或者是属性
  27. * 都必须要先获取这个元素
  28. */
  29. function fun1(){
  30. var img = document.getElementById("img")
  31. img.src = "img/2.png"
  32. }
  33. </script>
  34. </html>