Movatterモバイル変換


[0]ホーム

URL:


  1. 面向开发者的 Web 技术
  2. Web API
  3. HTMLImageElement

此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。

View in EnglishAlways switch to English

HTMLImageElement

Baseline Widely available *

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨2015年7月⁩.

* Some parts of this feature may have varying levels of support.

HTMLImageElement 接口提供了特别的属性和方法 (在常规的HTMLElement之外,它也能通过继承使用) 来操纵<img> 元素的布局和图像。

must be a string

Constructor

Image()

Image() 构造器,带有两个可选的参数,分别表示资源的宽度和高度,创建了一个尚未被插入 DOM 树中的HTMLImageElement 实例。When called without parameters,new Image() is equivalent to callingdocument.createElement("img").

属性

从它的父元素HTMLElement 继承的属性。

HTMLImageElement.alt

一个DOMString 表示 HTML 属性alt,表明图像的后备描述内容,会在图像无法加载时显示。

HTMLImageElement.complete只读

返回一个Boolean 如果浏览器已经下载完毕,并且图像是受支持的图片类型、解码的过程中没有发生错误,则返回true。That means this value is alsotrue if the image has nosrc value indicating an image to load.

HTMLImageElement.crossOrigin

一个DOMString 表示这个 img 元素的 CORS 设置。参考CORS settings attributes。This may benull if CORS is not used.

HTMLImageElement.currentSrc只读

返回一个DOMString 表示加载当前显示的图像的 URL。这可能会改变,因为图像是调整,由于不断变化的条件,由任何media queries 的地方。

HTMLImageElement.decoding

An optionalDOMString representing a hint given to the browser on how it should decode the image. If this value is provided, it must be one of the possible permitted values:sync to decode the image synchronously,async to decode it asynchronously, orauto to indicate no preference (which is the default). Read thedecoding page for details on the implications of this property's values.

HTMLImageElement.height

一个整数,表示 HTML 属性height,说明了图像在 CSS 像素中渲染的高度。

HTMLImageElement.isMap

一个Boolean 表示 HTML 属性ismap,说明了图像是某个服务器端图像映射的一部分。This is different from a client-side image map, specified using an<img> element and a corresponding<map> which contains<area> elements indicating the clickable areas in the image. The imagemust be contained within an<a> element; see theismap page for details.

HTMLImageElement.naturalHeight只读

返回一个整数,如果可用的话,表明图像在 CSS 中固有的高度,单位为像素;否则返回0。如果图片是以其原来的大小渲染,则此值等于图片的高度。

HTMLImageElement.naturalWidth只读

返回一个整数,如果可用的话,表明图像在 CSS 中固有的宽度,单位为像素;否则返回0。如果图片是以其原来的大小渲染,则此值等于图片的宽度。

HTMLImageElement.referrerPolicy

ADOMString that reflects thereferrerpolicy HTML attribute, which tells theuser agent how to decide which referrer to use in order to fetch the image. Read this article for details on the possible values of this string.

HTMLImageElement.sizes实验性

ADOMString reflecting thesizes HTML attribute. This string specifies a list of comma-separated conditional sizes for the image; that is, for a given viewport size, a particular image size is to be used. Read the documentation on thesizes page for details on the format of this string.

HTMLImageElement.src

一个DOMString 表示 HTML 属性src,包含图像的完整的 URL,包含图像的基础 URL。

HTMLImageElement.srcset实验性

一个DOMString 表示 HTML 属性srcset,包含了候选图像列表,用逗号分隔(',', U+002C COMMA)。一个候选的图像是一个 URL 跟着一个'w' 表示图像的宽度,或者一个'x' 表示像素密度。

HTMLImageElement.useMap

一个DOMString 表示 HTML 属性usemap,包含一个<map> 元素的页面本地 URL。The page-local URL is a pound (hash) symbol (#) followed by the ID of the<map> element, such as#my-map-element. The<map> in turn contains<area> elements indicating the clickable areas in the image.

HTMLImageElement.width

一个整数,表示 HTML 属性width,说明图像在 CSS 像素中渲染的宽度。

HTMLImageElement.x只读实验性

An integer indicating the horizontal offset of the left border edge of the image's CSS layout box relative to the origin of the<html> element's containing block.

HTMLImageElement.y只读实验性

The integer vertical offset of the top border edge of the image's CSS layout box relative to the origin of the<html> element's containing block.

已废弃的属性

HTMLImageElement.align已弃用

一个DOMString,表示图像如何与它周围的内容对齐。The possible values are"left","right","justify", and"center". This is obsolete; you should instead use CSS (such astext-align, which works with images despite its name) to specify the alignment.

HTMLImageElement.border已弃用

一个DOMString,表示图像边框的宽度。此属性已被弃用,应该用 CSSborder 属性来代替它。

HTMLImageElement.hspace已弃用

一个整数值,指定图像左右的留白,单位为像素。

HTMLImageElement.longDesc已弃用

一个USVString,specifying the URL at which a long description of the image's contents may be found. This is used to turn the image into a hyperlink automatically. Modern HTML should instead simply place an<img> inside an<a> element defining the hyperlink.

HTMLImageElement.lowSrc已弃用

一个USVString,specifying the URL of a low-quality (but faster to load) version of the same image. This was once used by browsers under constrained network conditions or on slow devices.

HTMLImageElement.name已弃用

一个DOMString,representing the name of the element.

HTMLImageElement.vspace已弃用

一个整数值,指定图像上下的留白,单位为像素。

方法

从它的父元素HTMLElement 继承的方法。

HTMLImageElement.decode()

Returns aPromise that resolves when the image is decoded and it's safe to append the image to the DOM. This prevents rendering of the next frame from having to pause to decode the image, as would happen if an undecoded image were added to the DOM.

错误

  • Thesrc attribute is empty ornull.
  • The specifiedsrc URL is the same as the URL of the page the user is currently on.
  • The specified image is corrupted in some way that prevents it from being loaded.
  • The specified image's metadata is corrupted in such a way that it's impossible to retrieve its dimensions, and no dimensions were specified in the<img> element's attributes.
  • The specified image is in a format not supported by theuser agent.

If an error occurs while trying to load or render the image, and anonerror event handler has been configured to handle theerror event, that event handler will get called. This can happen in a number of situations, including:

例子

js
var img1 = new Image(); // Image 构造器img1.src = "image1.png";img1.alt = "alt";document.body.appendChild(img1);var img2 = document.createElement("img"); // 使用 DOM HTMLImageElementimg2.src = "image2.jpg";img2.alt = "alt text";document.body.appendChild(img2);// 使用文档中的第一个 imgalert(document.images[0].src);

规范

Specification
HTML
# htmlimageelement

浏览器兼容性

参见

  • 实现了这个接口的 HTML 元素:<img>

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp