javascript - 在javascript中将源添加到HTML5视频?

标签 javascript html html5-video

我正在为 html5 视频运行以下代码。

var media;
function videotr(){
  media = document.createElement('video');
  media.preload = true;
  media.id = 'it'; 
  document.getElementById('crop').appendChild(media)
  return media;
}                
var div = document.getElementById('crop');
$(div).empty();
this.image = new videotr();
this.image.src = args.src;

但是,我想实现多个来源以实现兼容性。如何通过 java 脚本添加多个源? 我想以正确的方式执行此操作,而不是通过 innerHtml。

最佳答案

创建一个新的 source 元素并将其附加到 video 元素:

function addSourceToVideo(element, src, type) {
    var source = document.createElement('source');

    source.src = src;
    source.type = type;

    element.appendChild(source);
}

var video = document.createElement('video');

document.body.appendChild(video);

addSourceToVideo(video, 'http://upload.wikimedia.org/wikipedia/commons/7/79/Big_Buck_Bunny_small.ogv', 'video/ogg');

video.play();

这是一个 fiddle

关于javascript - 在javascript中将源添加到HTML5视频?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7192098/

相关文章:

javascript - 调用 Javascript 函数两次

javascript - JS : Loop through divs of certain class and modify child Divs based on parent divs attributes?

JavaScript 数学计算 - 与 Excel 公式相比结果不同

html - 悬停时父高度的子高度

html - CSS 显示 :table-cell; is not displaying at 100% of the width of the outer <dl>

javascript - Flowplayer 中的 MP4 视频无法在 Windows 7 Chrome 中播放

javascript - 使用 php 将在 javascript 中创建的 XMLDocument 对象保存在服务器上

javascript - 倒计时时钟 (JS)

ios - 减少/消除 <video> 元素上的缓冲滞后 (iOS)

javascript - html5-视频下载进度仅适用于 Firefox