wil*_*fun 31 jquery html5 html5-video
我有一个HTML视频,我想在视频结束时运行一个功能.我尝试过以下但我什么都没得到:
$(document).ready(function(){
$('video').live('ended',function(){
alert('video ended');
});
});
Run Code Online (Sandbox Code Playgroud)
任何想法为什么这不会在视频结束时触发?或者我没有提供足够的信息来回答这里的任何问题?
注意:我正在使用live()函数而不是on(),因为需要使用此项目的jQuery 1.7.
fbo*_*oes 57
为了您的粘贴和复制乐趣jQuery解决方案:
<video width="320" height="240">
<source src="movie.mp4" type="video/mp4">
</video>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>/*<![CDATA[*/
$(document).ready(function(){
$('video').on('ended',function(){
console.log('Video has ended!');
});
});
/*]]>*/</script>Run Code Online (Sandbox Code Playgroud)
将'video'-part 替换为视频的正确选择器.
编辑:这是没有jQuery的解决方案:
<video width="320" height="240">
<source src="movie.mp4" type="video/mp4">
</video>
<script>/*<![CDATA[*/
document.querySelector('video').addEventListener('ended',function(){
console.log('Video has ended!');
}, false);
/*]]>*/</script>Run Code Online (Sandbox Code Playgroud)
将'video'-part 替换为视频的正确选择器.
Ily*_*bin 12
<html>
<head>
<title></title>
<script>
function videoEnded() {
alert('video ended');
}
</script>
</head>
<body>
<video src="video/endscene.mp4" id="video" controls onended="videoEnded()">
video not supported
</video>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
使用onended事件
var video = document.getElementsByTagName('video')[0];
video.onended = function(e) {
alert('video ended');
}
Run Code Online (Sandbox Code Playgroud)
小智 5
您可以尝试以下方法:
$(document).on('ended', 'video', function (e) {
alert('video ended');
});
Run Code Online (Sandbox Code Playgroud)
你说的是在加载DOM后动态插入视频。在此脚本中,您在 DOM 上搜索video元素并将其与ended函数绑定。
我希望这有帮助!
小智 5
我管理它工作的唯一方法是以下代码:
$('video')[0].on('ended',function(){
console.log('Video has ended!');
});
Run Code Online (Sandbox Code Playgroud)