HTMLImageElement
property
src
, which reflects the HTML
src
attribute, specifies the image to display in the
<img>
元素。
htmlImageElement.src = newSource; let src = htmlImageElement.src;
When providing only a single image, rather than a set of images from which the browser selects the best match for the viewport size and display pixel density, the
src
attribute is a
USVString
specifying the URL of the desired image. This can be set either within the HTML itself using the
src
content attribute, or programmatically by setting the element's
src
特性。
若使用
srcset
content attribute to provide multiple image options for different display pixel densities, the URL specified by the
src
attribute is used in one of two ways:
srcset
.
srcset
with the size multiplier
1x
; that is, the image specified by
src
is used on low-density screens (such as typical 72 DPI or 96 DPI displays).
Additionally, if you use
src
along with
both
sizes
(or the corresponding
sizes
content attribute)
and
srcset
in order to choose an image based on the viewport size, the
src
attribute is only used as a fallback for browsers that don't support
sizes
and
srcset
; otherwise, it's not used at all.
<img src="https://interactive-examples.mdn.mozilla.net/media/examples/grapefruit-slice-332-332.jpg"
width="160"
alt="Slices of grapefruit, looking yummy.">
When using a set of images with the
srcset
特性,
src
serves as either a fallback for older browsers, or as the
1x
size of the image.
When using viewport-bases selection of an image from a
srcset
by also specifying the
sizes
特性,
src
property serves as the fallback to be used on browsers that don't support viewport-based selection. Browsers that
do
support viewport-based selection will ignore
src
in this situation.
| 规范 | 状态 | 注释 |
|---|---|---|
|
HTML 实时标准
The definition of 'HTMLImageElement.src' in that specification. |
实时标准 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
src
|
Chrome 1 | Edge 12 | Firefox Yes | IE ? | Opera Yes | Safari Yes | WebView Android Yes | Chrome Android Yes | Firefox Android Yes | Opera Android Yes | Safari iOS Yes | Samsung Internet Android Yes |
完整支持
兼容性未知
HTMLImageElement
BeforeUnloadEvent
DOMStringMap
ErrorEvent
GlobalEventHandlers
HTMLAnchorElement
HTMLAreaElement
HTMLAudioElement
HTMLBRElement
HTMLBaseElement
HTMLBaseFontElement
HTMLBodyElement
HTMLButtonElement
HTMLCanvasElement
HTMLContentElement
HTMLDListElement
HTMLDataElement
HTMLDataListElement
HTMLDialogElement
HTMLDivElement
HTMLDocument
HTMLElement
HTMLEmbedElement
HTMLFieldSetElement
HTMLFormControlsCollection
HTMLFormElement
HTMLFrameSetElement
HTMLHRElement
HTMLHeadElement
HTMLHeadingElement
HTMLHtmlElement
HTMLIFrameElement
HTMLInputElement
HTMLIsIndexElement
HTMLKeygenElement
HTMLLIElement
HTMLLabelElement
HTMLLegendElement
HTMLLinkElement
HTMLMapElement
HTMLMediaElement
HTMLMetaElement
HTMLMeterElement
HTMLModElement
HTMLOListElement
HTMLObjectElement
HTMLOptGroupElement
HTMLOptionElement
HTMLOptionsCollection
HTMLOutputElement
HTMLParagraphElement
HTMLParamElement
HTMLPictureElement
HTMLPreElement
HTMLProgressElement
HTMLQuoteElement
HTMLScriptElement
HTMLSelectElement
HTMLShadowElement
HTMLSourceElement
HTMLSpanElement
HTMLStyleElement
HTMLTableCaptionElement
HTMLTableCellElement
HTMLTableColElement
HTMLTableDataCellElement
HTMLTableElement
HTMLTableHeaderCellElement
HTMLTableRowElement
HTMLTableSectionElement
HTMLTemplateElement
HTMLTextAreaElement
HTMLTimeElement
HTMLTitleElement
HTMLTrackElement
HTMLUListElement
HTMLUnknownElement
HTMLVideoElement
HashChangeEvent
历史
ImageData
定位
MessageChannel
MessageEvent
MessagePort
Navigator
NavigatorGeolocation
NavigatorID
NavigatorLanguage
NavigatorOnLine
NavigatorPlugins
PageTransitionEvent
Plugin
PluginArray
PopStateEvent
PortCollection
PromiseRejectionEvent
RadioNodeList
Transferable
ValidityState
Window
WindowBase64
WindowEventHandlers
WindowTimers