Javascript 如何使用 jQuery 更改 html5 视频中的源
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14799172/
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
How to change source in html5 video with jQuery
提问by Lukas
I have an html5 video streaming and I need to change it source* after some click action. So what i'm doing works, but only in html, i can see source change but it no changing on my display, can you help me? What is wrong? *The source is appending frome xml file.
我有一个 html5 视频流,我需要在一些点击操作后更改它的源*。所以我正在做的工作,但仅在 html 中,我可以看到源代码更改,但在我的显示器上没有更改,你能帮我吗?怎么了?*源是从 xml 文件中附加的。
HTML
HTML
<video autoplay loop width="960" height="540" id="video">
<source src="video/movie_01.mp4" id="tv_main_channel">
</video>
JS
JS
btn.on('click', function(){
var tv_main_channel = $('#tv_main_channel'),
d_program_source_mp4 = $(program_self).find("program_source_mp4").text();
tv_main_channel.attr('src', d_program_source_mp4);
}
also i try it with appendbut it still not work
我也试过了,append但还是不行
var video_block = $('#video');
video_block.empty();
video_block.append(
'<source src="'+ d_program_source_mp4 +'">'
);
Thx for help.
谢谢你的帮助。
采纳答案by Eric Martins
It's simple, just do this:
很简单,只要这样做:
btn.on("click", function(){
var src = "new_video_src.mp4";
$("#video").find("#tv_main_channel").attr("src", src)
})
回答by richie
See this fiddle: http://jsfiddle.net/qGbzb/2/
看到这个小提琴:http: //jsfiddle.net/qGbzb/2/
To dynamically load videos you need to run
要动态加载视频,您需要运行
var video_block = $('#video');
video_block.load();
Then you should also see a change in the display too, and not only in the html.
然后你也应该看到显示的变化,而不仅仅是在 html 中。
回答by Manu M
You can use like this in click event
您可以在点击事件中像这样使用
var src = "new_video_src.mp4";
$("#tv_main_channel").src=src;
This is the actual method for video tag elements
这是视频标签元素的实际方法

