Image src attribute

Definition and usage

src Attribute to set or return the image src attribute value.

Required src attribute Specify the image URL.

Tip:You can change the src attribute at any time. However, if new height and width attributes are not specified, the new image will inherit the height and width attributes of the original image.

Example

Example 1

Change the image URL:

document.getElementById("myImg").src = "wuhan.jpg";

Try it yourself

Example 2

Return the image URL:

var x = document.getElementById("myImg").src;

Try it yourself

Syntax

Return src attribute:

imageObject.src

Set src attribute:

imageObject.src = URL

Attribute value

Value Description
URL

Specify the image URL.

Possible values:

  • Absolute URL - Pointing to another website (such as src="http://www.example.com/default.htmls")
  • 相对 URL - 指向网站内的文件(如 src="default.html")

技术细节

返回值: 字符串值,表示图像的 URL。返回整个 URL,包括协议(如 http://)。

浏览器支持

Chrome Edge Firefox Safari Opera
Chrome Edge Firefox Safari Opera
支持 支持 支持 支持 支持

相关页面

HTML 参考手册:HTML <img> src 属性