jQuery html5-video by button $(...).play 不是一个函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15466174/
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
html5-video by button $(...).play is not a function
提问by Shenya
I try to play videos one after the other via button or automatically at the end of the video. by this code:
我尝试通过按钮一个接一个地播放视频或在视频结束时自动播放。通过此代码:
//automatically play
$("#videoPlayer").bind('ended', function() {
if(cnt <= 10 && bNum == 0) cnt++;
$('#videoPlayer').attr('poster','./media/spot'+cnt+'.jpg').html('<source src="./media/spot'+cnt+'.mp4" type="video/mp4"><source src="./media/spot'+cnt+'.ogg" type="video/ogg"><source src="./media/spot'+cnt+'.webm" type="video/webm">');
$('#video-title').html('Spot '+cnt);
if(cnt < 10) {
this.load();
this.play();
cnt++;
}
bNum = 1;
if(cnt >= 10) $('.link1').remove();
});
//Play by the button
$('.link1').on('click', function() {
if(cnt < 10 && bNum == 0) cnt++;
$(this).attr('rel', cnt).attr('title', 'Spot '+cnt);
$('#videoPlayer').attr('poster','./media/spot'+cnt+'.jpg').html('<source src="./media/spot'+cnt+'.mp4" type="video/mp4"><source src="./media/spot'+cnt+'.ogg" type="video/ogg"><source src="./media/spot'+cnt+'.webm" type="video/webm">');
$('#video-title').html('Spot '+cnt);
if(cnt >= 10) $('.link1').remove();
if(cnt <= 10) {
if($('#videoPlayer').load()) $('#videoPlayer').play();
cnt++;
}
});
the html part is:
html部分是:
<video width="640" id="videoPlayer" autoplay preload="metadata" poster="./media/spot1.jpg">
<source src="./media/spot1.ogg" type="video/ogg">
<source src="./media/spot1.mp4" type="video/mp4">
<source src="./media/spot1.webm" type="video/webm">
Your browser does not support the video tag.
</video></div>
<ul>
<li class="link1" rel="2">N?chster Spot </li>
</ul>
</div>
the automatic part works well without an error. But the part by button gets an error at the line;
自动部分运行良好,没有错误。但是 part by 按钮在该行出现错误;
$('#videoPlayer').play();
with the message
随着消息
"$(...).play is not a function".
I can't find why. (For this.play()
I can also write $('videoPlayer')
in the automatic part. It is the same.)
我找不到原因。(因为this.play()
我也可以写$('videoPlayer')
在自动部分。它是一样的。)
回答by Musa
$('#videoPlayer')
gives you a jQuery object and the jQuery object does not have a play
method. The play method you are looking for is in the native dom element inside the jQuery object. To get access to the element inside just use array syntax or .get()
. e.g. $('#videoPlayer')[0].play();
or $('#videoPlayer').get(0).play();
$('#videoPlayer')
给你一个 jQuery 对象,而 jQuery 对象没有play
方法。您正在寻找的播放方法位于 jQuery 对象内的本机 dom 元素中。要访问内部元素,只需使用数组语法或.get()
. 例如$('#videoPlayer')[0].play();
或$('#videoPlayer').get(0).play();