2016-09-27 16 views
0

播放的視頻在我的應用我有我我附加SRC視頻標籤動態無法在HTML5

這是我的代碼

<video autobuffer controls autoplay> 
    <source id="howtovideoimg" src="" type="video/mp4"> 
</video> 


$(document).ready(function() { 
$('#howtovideoimg').attr('src', 'http://www.quirksmode.org/html5/videos/big_buck_bunny.mp4'); 
}); 

這是我的小提琴

http://jsfiddle.net/cod7ceho/125/

請讓我知道如何播放視頻

+0

[更改源的可能的複製在html5視頻標籤](http://stackoverflow.com/questions/5235145/changing-source-on-html5-video-tag) – insertusernamehere

+0

「src」不是視頻標籤的屬性。它需要一個''標籤。 http://www.w3schools.com/html/html5_video.asp。 – ADyson

+1

我更新了小提琴http://jsfiddle.net/cod7ceho/127/ – nmanikiran

回答

1

$(document).ready(function() { 
 
    var player = document.getElementById('videoPlayer'); 
 
     var filepath = document.getElementById('videoPlayer'); 
 
     filepath.src = "http://www.quirksmode.org/html5/videos/big_buck_bunny.mp4"; 
 

 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<video autobuffer controls autoplay id="videoPlayer"> 
 
    <source id="howtovideoimg" src="" type="video/mp4"> 
 
</video>

0

您必須創建一個新的<source>元素,然後將其追加到視頻標籤,這是簡單的在JavaScript動態添加的元素,我把js代碼一個例子,我覺得能有所幫助你...

function addSourceElement() { 
    var source = document.createElement('source'); 
    var video = document.createElement('video'); 
    source.src = 'http://www.quirksmode.org/html5/videos/big_buck_bunny.mp4'; 
    source.type = 'video/mp4'; 

    video.appendChild(source); 
    document.body.appendChild(video); 
    video.play(); 
} 
0

只需使用這種JS的:

$('#howtovideoimg source').attr('src', "http://www.quirksmode.org/html5/videos/big_buck_bunny.mp4"); 

上這個網站:

<video id="howtovideoimg" autobuffer controls autoplay> 
    <source src="" type="video/mp4"> 
</video> 

我使用在頁面加載(白色視頻)源的值,以避免瀏覽器的一些奇怪的行爲不考慮源屬性,如果它是空的