這篇文章主要介紹了JS動態修改圖片的URL(src)的方法,涉及javascript操作圖片src屬性的技巧,非常具有實用價值,需要的朋友可以參考下
本文實例講述了JS動態修改圖片的URL(src)的方法。分享給大家供大家參考。具體如下:
下面的JS代碼可以動態修改圖片地址,以顯示一張新的圖片,實際上是通過修改圖片的src屬性來實現的
- <!DOCTYPE html>
- <html>
- <head>
- <script>
- function changeSrc()
- {
- document.getElementById("myImage").src="hackanm.gif";
- }
- </script>
- </head>
- <body>
- <img id="myImage" src="compman.gif" width="107" height="98">
- <br><br>
- <input type="button" onclick="changeSrc()" value="Change image">
- <p><b>Note:</b> The src property can be changed at any time.
- However, the new image inherits the height and width
- attributes of the original image,
- if not new height and width properties are specified.</p>
- </body>
- </html>
希望本文所述對大家的javascript程序設計有所幫助。
新聞熱點
疑難解答
圖片精選