简体   繁体   English

使用javascript / jquery更改html5视频源

[英]change a html5 video source using javascript/jquery

I have a video on my webpage like so: 我的网页上有一个视频,像这样:

<div id="videoContainer">
<video id="video1" width="960" controls autoplay>
    <source src=static/media/Wreck_It_Ralph_Trailer.mp4 type="video/mp4">
    Your browser does not support html5 video
</video>

and I want to change the source of the video using jquery/javascript. 并且我想使用jquery / javascript更改视频的来源。 I have this code but it doesn't work 我有此代码,但它不起作用

$('source').attr({'src':'static/media/Here_Comes_The_Boom_Official_Trailer.mp4'})

Can someone say why this isn't working or suggest a correct way to do it? 有人可以说为什么这行不通或提出正确的做法吗?

After looking at the link by Mic I needed an extra line of code 在查看了Mic的链接之后,我需要额外的一行代码

a=document.getElementById('video1')
a.load()

I believe you can do something like this: 我相信你可以做这样的事情:

document.getElementById('video1').src = 'static/media/Here_Comes_The_Boom_Official_Trailer.mp4';

which just changes the video player's source directly, without fiddling with the DOM elements. 只需直接更改视频播放器的源即可,而无需摆弄DOM元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM