2012-03-25 211 views
6

我想用jquery播放/暂停视频。使用jquery播放/暂停HTML5视频

这里是我的代码:

(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); 

回答

3

添加安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方法d其DOM方法

DEMO

+0

@ 3nigma,再次感谢我的排序问题。 – coder 2012-03-25 21:38:53

+0

很高兴帮助,从上一个问题我注意到,你使用的锚点和视频控制中的多个ID,这是错误的ids​​应该是唯一的... – Rafay 2012-03-25 21:40:00

+0

当然,我不会再那样做。感谢指出。 – coder 2012-03-25 21:41:05