操作元素之修改元素属性

以你之姓@ 2023-10-03 14:27 85阅读 0赞

操作元素之修改元素属性

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title>操作元素之修改元素属性</title>
  8. <style>
  9. img {
  10. width: 300px;
  11. }
  12. </style>
  13. </head>
  14. <body>
  15. <button id="ldh">刘德华</button>
  16. <button id="zxy">张学友</button> <br>
  17. <img src="https://pic.imgdb.cn/item/6120ef224907e2d39c002925.jpg" alt="" title="刘德华">
  18. <script>
  19. // 修改元素属性 src
  20. // 1. 获取元素
  21. var ldh = document.getElementById('ldh');
  22. var zxy = document.getElementById('zxy');
  23. var img = document.querySelector('img');
  24. // 2. 注册事件 处理程序
  25. zxy.onclick = function () {
  26. img.src = 'https://pic.imgdb.cn/item/6120ef464907e2d39c007048.jpg';
  27. img.title = '张学友';
  28. }
  29. ldh.onclick = function () {
  30. img.src = 'https://pic.imgdb.cn/item/6120ef224907e2d39c002925.jpg';
  31. img.title = '刘德华';
  32. }
  33. </script>
  34. </body>
  35. </html>

发表评论

表情:
评论列表 (有 0 条评论,85人围观)

还没有评论,来说两句吧...

相关阅读