Javascript 使用jQuery控制视频标签
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4059640/
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
Use jQuery to control video tag
提问by Benjamin Allison
So, I want to use a jQuery function to collect a URL from a link's REL, and pass it to a element.
因此,我想使用 jQuery 函数从链接的 REL 中收集 URL,并将其传递给元素。
Collecting the REL and sending it to the is no problem... but what's required to trigger the element's load and play functions, from jQuery?
收集 REL 并将其发送到 没有问题……但是从 jQuery 触发元素的加载和播放功能需要什么?
Here's what I have so far:
这是我到目前为止所拥有的:
$(function(){
$('a.componentLink').click(function() {
event.preventDefault();
var vidURL = $(this).attr('rel');
$('#myVideo > source').attr('src', vidURL);
$('#myVideo').load();
$('#myVideo').play();
})
});
ATM, it doesn't play... I assume jQuery doesn't natively have access to the play function... but there must be a way around that.
ATM,它不播放...我认为 jQuery 本身无法访问播放功能...但必须有办法解决这个问题。
回答by lonesomeday
You need to call .play()
on the element itself, not on the jQuery selection. Do the following:
您需要调用.play()
元素本身,而不是 jQuery 选择。请执行下列操作:
$(function(){
$('a.componentLink').click(function() {
event.preventDefault();
var vidURL = $(this).attr('rel');
$('#myVideo').attr('src', vidURL);
var video = $('#myVideo').get(0);
video.load();
video.play();
})
});