大网数据广告活动
Web 标准与前端开发

HTMLMediaElement: textTracks property

· 52CE 我爱测

The read-only **`textTracks`** property on HTMLMediaElement objects returns a TextTrackList object listing all of the TextTrack objects representing the media element's text tracks, in the same order as in the list of text tracks.

HTML DOM

The read-only textTracks property on HTMLMediaElement objects returns a TextTrackList object listing all of the TextTrack objects representing the media element's text tracks, in the same order as in the list of text tracks.

You can detect when tracks are added to and removed from an <audio> or <video> element using the addtrack and removetrack events. However, these events aren't sent directly to the media element itself. Instead, they're sent to the track list object of the HTMLMediaElement that corresponds to the type of track that was added to the element

The returned list is live; that is, as tracks are added to and removed from the media element, the list's contents change dynamically. Once you have a reference to the list, you can monitor it for changes to detect when new text tracks are added or existing ones removed.

See TextTrackList events to learn more about watching for changes to a media element's track list.

Value

A TextTrackList object representing the list of text tracks included in the media element. The list of tracks can be accessed using textTracks[n] to get the n-th text track from the object's list of text tracks, or using the textTracks.getTrackById() method.

Each track is represented by a TextTrack object which provides information about the track.

Examples

We start with a <video> that has several <track> children

<video controls>
  <source src="/shared-assets/videos/sintel-short.webm" type="video/webm" />
  <source src="/shared-assets/videos/sintel-short.mp4" type="video/mp4" />
  <track
    kind="subtitles"
    src="/shared-assets/misc/sintel-en.vtt"
    srclang="en"
    label="English" />
  <track
    kind="subtitles"
    src="/shared-assets/misc/sintel-de.vtt"
    srclang="de"
    label="Deutsch" />
  <track
    kind="subtitles"
    src="/shared-assets/misc/sintel-es.vtt"
    srclang="es"
    label="Español" />
</video>

The HTMLMediaElement.textTracks returns a TextTrackList through which we can iterate. Here we set all three tracks to show simultaneously.

const tracks = document.querySelector("video").textTracks;

for (const track of tracks) {
  track.mode = "showing";
}

Examples

Specifications

Browser compatibility

See also

  • HTMLMediaElement: Interface used to define the HTMLMediaElement.textTracks property
  • audio, video
  • AudioTrack, AudioTrackList
  • VideoTrack, VideoTrackList
  • addtrack, change, removetrack: AudioTrackList events
  • addtrack, change, removetrack: VideoTrackList events

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。