---
title: 'HTMLMediaElement: durationchange 事件'
slug: Web/API/HTMLMediaElement/durationchange_event
tags:
- Audio
- Event
- HTMLMe
- HTMLMediaElement
- Reference
- Video
translation_of: Web/API/HTMLMediaElement/durationchange_event
---
{{APIRef("HTMLMediaElement")}}
durationchange
事件会在 duration
属性更新时被触发。
Bubbles |
No |
Cancelable |
No |
Interface |
{{DOMxRef("Event")}} |
Target |
Element |
Default Action |
None |
Event handler property |
{{domxref("GlobalEventHandlers.ondurationchange")}} |
Specification |
HTML5 media |
例子
下面的例子为 HTMLMediaElement 的 durationchange
事件添加事件监听器,然后在事件触发时发送一个消息。
使用 addEventListener()
:
const video = document.querySelector('video');
video.addEventListener('durationchange', (event) => {
console.log('Not sure why, but the duration of the video has changed.');
});
使用 ondurationchange
事件处理器属性:
const video = document.querySelector('video');
video.ondurationchange = (event) => {
console.log('Not sure why, but the duration of the video has changed.');
};
Specifications
Specification |
Status |
{{SpecName('HTML WHATWG', "media.html#event-media-durationchange", "durationchange media event")}} |
{{Spec2('HTML WHATWG')}} |
{{SpecName('HTML5 W3C', "embedded-content-0.html#event-media-durationchange", "durationchange media event")}} |
{{Spec2('HTML5 W3C')}} |
Browser compatibility
{{Compat("api.HTMLMediaElement.durationchange_event")}}
- {{domxref("HTMLMediaElement.playing_event", 'HTMLMediaElement: playing event')}}
- {{domxref("HTMLMediaElement.waiting_event", 'HTMLMediaElement: waiting event')}}
- {{domxref("HTMLMediaElement.seeking_event", 'HTMLMediaElement: seeking event')}}
- {{domxref("HTMLMediaElement.seeked_event", 'HTMLMediaElement: seeked event')}}
- {{domxref("HTMLMediaElement.ended_event", 'HTMLMediaElement: ended event')}}
- {{domxref("HTMLMediaElement.loadedmetadata_event", 'HTMLMediaElement: loadedmetadata event')}}
- {{domxref("HTMLMediaElement.loadeddata_event", 'HTMLMediaElement: loadeddata event')}}
- {{domxref("HTMLMediaElement.canplay_event", 'HTMLMediaElement: canplay event')}}
- {{domxref("HTMLMediaElement.canplaythrough_event", 'HTMLMediaElement: canplaythrough event')}}
- {{domxref("HTMLMediaElement.durationchange_event", 'HTMLMediaElement: durationchange event')}}
- {{domxref("HTMLMediaElement.timeupdate_event", 'HTMLMediaElement: timeupdate event')}}
- {{domxref("HTMLMediaElement.play_event", 'HTMLMediaElement: play event')}}
- {{domxref("HTMLMediaElement.pause_event", 'HTMLMediaElement: pause event')}}
- {{domxref("HTMLMediaElement.ratechange_event", 'HTMLMediaElement: ratechange event')}}
- {{domxref("HTMLMediaElement.volumechange_event", 'HTMLMediaElement: volumechange event')}}
- {{domxref("HTMLMediaElement.suspend_event", 'HTMLMediaElement: suspend event')}}
- {{domxref("HTMLMediaElement.emptied_event", 'HTMLMediaElement: emptied event')}}
- {{domxref("HTMLMediaElement.stalled_event", 'HTMLMediaElement: stalled event')}}
See Also
- {{domxref("HTMLAudioElement")}}
- {{domxref("HTMLVideoElement")}}
- {{HTMLElement("audio")}}
- {{HTMLElement("video")}}