有关video.currentTime事件绑定的问题

时间:2014-12-20 12:47:47

标签: javascript html5 events video

我想在视频达到5秒或更长时间后触发一个事件,但我在使用代码时遇到了一些问题。

HTML:

<video id="video1" controls>
    <source src="media/video.m4v" type="video/mp4" />
    <source src="media/video.webm" type="video/webm" />
    <source src="media/video.ogg" type="video/ogg" />
    <p> Your browser does not support the HTML5 video feature. </p>
</video>

使用Javascript:

 $(function () {

    var video = $('#video1');
    var time = 5;
    function init () {
        alert('Video is not available.');
    }

    $(video).on('timeupdate', function () {
        if (video.currentTime >= time) {
            init();
        }
    });

 });

遗憾的是,这不是一个有效的代码,有人知道我哪里出错了吗?

1 个答案:

答案 0 :(得分:0)

你得到undefined,因为视频是没有属性currentTime的jQuery对象,你可以像这样得到这个值

video.get(0).currentTime

this.currentTime

实施例

 $(function () {

    var video = $('#video1');
    var time = 5;
    function init () {
        alert('Video is not available.');
    }

    video.on('timeupdate', function () {
        if (video.get(0).currentTime >= time) {
            init();
        }
    });

 });
相关问题