查找图像的源(src)

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. <img id="myImg" src="/i/shanghai_lupu_bridge.jpg" alt="上海卢浦大桥" width="350" height="234">
  6.  
  7. <p>单击按钮以显示图像的 src 属性。</p>
  8.  
  9. <button onclick="myFunction()">试一试</button>
  10.  
  11. <p id="demo"></p>
  12.  
  13. <script>
  14. function myFunction() {
  15. var x = document.getElementById("myImg").src;
  16. document.getElementById("demo").innerHTML = x;
  17. }
  18. </script>
  19.  
  20. </body>
  21. </html>