Javascript 视频 html5 元素上的播放事件是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12680432/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the play event on a video html5 element?
提问by Andres
I have a video html5 tag embedded on page. I need to trigger an action when the user clicks the "Play Button". But I can't find how to bind that to my action. Is there an event for what I need? I'm using jQuery...
我在页面上嵌入了一个视频 html5 标签。当用户单击“播放按钮”时,我需要触发一个动作。但我找不到如何将其绑定到我的行动。有我需要的活动吗?我正在使用 jQuery...
Thanks!
谢谢!
回答by Stephen Booher
Does this W3C demo page help? http://www.w3.org/2010/05/video/mediaevents.htmlIt appears the play event is simply 'play'.
这个 W3C 演示页面有帮助吗?http://www.w3.org/2010/05/video/mediaevents.html看来播放事件只是“播放”。
For example:
例如:
$('video').bind('play', function (e) {
// do something
});
or
或者
$('video').on('play', function (e) {
// do something
});
回答by Uri May
I used code from the following pageand stripped it:
我使用了以下页面中的代码并将其剥离:
<script language="javascript">
document.addEventListener("DOMContentLoaded", init, false);
function init() {
var _video = document.getElementById("video");
_video.addEventListener("playing", play_clicked, false);
}
function play_clicked() {
alert("play was clicked");
}
</script>
<video id='video'
controls preload='none'
poster="http://media.w3.org/2010/05/sintel/poster.png">
<source id='mp4'
src="http://media.w3.org/2010/05/sintel/trailer.mp4"
type='video/mp4'>
<p>Your user agent does not support the HTML5 Video element.</p>
</video>
Hoped I could help.
希望我能帮上忙。
回答by Jabare Mitchell
HTML5 Video elements have an onplay
event that you can use that doesn't require the extra step of adding a listener. This solution also doesn't require JQuery.
HTML5 Video 元素有一个onplay
您可以使用的事件,不需要添加侦听器的额外步骤。此解决方案也不需要 JQuery。
var myVid = document.getElementById('video');
if (myVid !== null) { // Possibility of no video loaded in DOM
myVid.onplay = function () {
//do something
};
}
MDNhas full details.
MDN有完整的细节。