Source - HTML(超文本标记语言) 编辑

概述

HTML <source> 元素为 <picture>, <audio> 或者 <video> 元素指定多个媒体资源。这是一个空元素。它通常用于以不同浏览器支持的多种格式提供相同的媒体内容。

Usage Context

A media element (<audio> or <video>, and it should be placed before any flow content or <track> element.
Permitted parent elementsA <picture> element, and it should be placed before the <img> element.
Content categoriesNone.
Permitted contentNone, it is an empty element.
Tag omissionIt must have start tag, but must not have an end tag.
DOM interfaceHTMLSourceElement

属性

该元素包含 全局属性.

sizes
Is a list of source sizes that describes the final rendered width of the image represented by the source. Each source size consists of a comma-separated list of media condition-length pairs. This information is used by the browser to determine, before laying the page out, which image defined in srcset to use.
The sizes attribute has an effect only when the <source> element is the direct child of a <picture> element.
src
Required for <audio> and <video>, address of the media resource. The value of this attribute is ignored when the <source> element is placed inside a <picture> element.
srcset
A list of one or more strings separated by commas indicating a set of possible images represented by the source for the browser to use. Each string is composed of:
  1. one URL to an image,
  2. a width descriptor, that is a positive integer directly followed by 'w'. The default value, if missing, is the infinity.
  3. a pixel density descriptor, that is a positive floating number directly followed by 'x'. The default value, if missing, is 1x.

Each string in the list must have at least a width descriptor or a pixel density descriptor to be valid. Among the list, there must be only one string containing the same tuple of width descriptor and pixel density descriptor.
The browser chooses the most adequate image to display at a given point of time.
The srcset attribute has an effect only when the <source> element is the direct child of a <picture> element.

type
The MIME-type of the resource, optionally with a codecs parameter. See RFC 4281 for information about how to specify codecs.
media
Media query of the resource's intended media; this should be used only in a <picture> element.

If the type attribute isn't specified, the media's type is retrieved from the server and checked to see if Gecko can handle it; if it can't be rendered, the next source is checked. If the type attribute is specified, it's compared against the types Gecko can play, and if it's not recognized, the server doesn't even get queried; instead, the next source element is checked at once.

Examples

This example demonstrates how to offer a video in Ogg format for users whose browsers support Ogg format, and a QuickTime format video for users whose browsers support that. If theaudio or video element is not supported by the browser, a notice is displayed instead.  If the browser supports the element but does not support any of the specified formats, an error event is raised and the default media controls (if enabled) will indicate an error. See also the list of media formats supported by the audio and video elements in various browsers.

<video controls>
  <source src="foo.webm" type="video/webm">
  <source src="foo.ogg" type="video/ogg">
  <source src="foo.mov" type="video/quicktime">
  I'm sorry; your browser doesn't support HTML5 video.
</video>

For more examples, see Using audio and video in Firefox.

Specifications

SpecificationStatusComment
HTML Living Standard
<source>
Living StandardInitial definition of <source> used inside a <picture> element.
HTML Living Standard
<source>
Living StandardInitial definition of <source> used inside a media element, <audio> or <video>.

Browser compatibility

We're converting our compatibility data into a machine-readable JSON format. This compatibility table still uses the old format, because we haven't yet converted the data it contains. Find out how you can help!
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support(Yes)3.5 (1.9.1)9.0(Yes)(Yes)
media attribute(Yes)15.0 (15.0)9.0(Yes)(Yes)
sizes attribute?33 (33)[1]???
srcset attribute?33 (33)[1]???
FeatureAndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support(Yes)1.0 (1.0)???
media attribute(Yes)15.0 (15.0)???
sizes attribute?33.0 (33)[1]???
srcset attribute?33.0 (33)[1]???

[1] This feature is behind the dom.image.picture.enabled preference, defaulting to false.

Currently, only a small subset of the functionality is implemented — Gecko picks the first source element that has a type matching the MIME-type of a supported media format; see bug 449363 for details.

See also

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:70 次

字数:15072

最后编辑:7年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文