使用Jquery播放/暂停HTML5视频

7

我希望使用jquery播放/暂停视频。

这是我的代码:

(function ($) {
    // plugin definition
    $.fn.htmlvideo = function (options) {
        // build main options before element iteration
        var defaults = {
            theme: 'normal',
        };
        var options = $.extend(defaults, options);
        // iterate and reformat each matched element
        return this.each(function () {
            var $htmlvideo = $(this);

            addvideo();
            addcontrols();


            function addvideo() {
                var addvideo = $('<video width="1000"><source src="http://devfiles.myopera.com/articles/2642/sintel-trailer.ogv" type="video/ogg; codecs="theora, vorbis""><source src="http://devfiles.myopera.com/articles/2642/sintel-trailer.mp4" type="video/mp4; codecs="avc1.42E01E, mp4a.40.2""></video>');
                $(addvideo).appendTo('#video');
            }

            function addcontrols() {
                var controls = $('<div id="controls" class="controls"><button id="playbtn" class="playbtn"></button></div>');
                $(controls).appendTo('#controlspane');
            }

            $('.playbtn').click(function () {
               //Here I need to make the video play
            });


        });
    };
})(jQuery);
2个回答

4

将ID添加到视频控件

function addvideo() {
            var addvideo = $('<video controls="controls" width="480" height="208" id="videoo"><source src="http://devfiles.myopera.com/articles/2642/sintel-trailer.ogv" type="video/ogg; codecs="theora, vorbis""><source src="http://devfiles.myopera.com/articles/2642/sintel-trailer.mp4" type="video/mp4; codecs="avc1.42E01E, mp4a.40.2""></video>');
             $(addvideo).appendTo('body');


        }

使用委托,因为您添加的按钮是动态的

$(document).delegate('.playbtn',"click",function () {    
        $('#videoo')[0].play();        
});

$("#videoo")[0] 返回的是DOM元素而不是jQuery对象,因为play方法不是jQuery方法而是DOM方法。

演示


@3nigma-再次感谢您为我解决问题。 - coder
很高兴能帮上忙。从之前的问题中我注意到你在锚点和视频控件中使用了多个id,这是错误的,每个id应该是唯一的。 - Rafay

2

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接