动态插入 html5 视频元素在 Opera 中不起作用

发布于 2024-10-06 02:37:53 字数 276 浏览 0 评论 0原文

我正在使用 JavaScript 将视频元素插入到 DOM 中,但它在 Opera (v10.63) 中不起作用。元素已插入,但视频未出现/播放。适用于 Firefox、Chrome 和 Safari。我仍在运行一些测试,看看是否可以让它工作,但我只是想知道是否有其他人遇到过这个问题。

另一方面,可能对其他人有帮助的一点是,事件委托似乎只能在 Firefox 上进行,而不能在 Opera、Chrome 或 Safari 上进行。这是基于我所做的代码测试,因此它可能是错误的,但我无法让它工作。

非常感谢

I'm using JavaScript to insert video elements into the DOM but it doesn't work in Opera (v10.63). The element gets inserted but the video doesn't appear/play. Works in Firefox, Chrome and Safari. I'm still running some tests to see if I can get it to work but I just wondered whether anyone else had come across this issue.

On another note which might be helpful to others, it seems event delegation is only possible with Firefox and not Opera, Chrome or Safari. That's based on code tests I've done so it could be wrong but I can't get it to work.

Many thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

把时间冻结 2024-10-13 02:37:54

在 Opera 10.63 中可以使用 JavaScript 将视频元素添加到 DOM。例如,下面的代码应该可以工作(我在 Opera 10.61 和 Opera 11 中测试了它)。

var vid, makevid;
makevid = function(e){
    vid = document.createElement('video');
    vid.src = 'path/to/an/oggtheorafile.ogv';
        vid.setAttribute('autoplay',true);
    document.getElementsByTagName('body')[0].appendChild(vid);
} 
window.addEventListener('load',makevid,false);

不过,您必须确保使用兼容的视频格式。 Opera 10.63 支持 Ogg/Theora 和 WebM。如果您使用 MPEG4/H.264,该视频将无法在 Opera 中播放。

(编辑以更正 Opera 10.6x 中的视频格式支持。它确实支持 WebM)

编辑 2:请注意,如果您使用 JavaScript 在 DOM 中插入视频元素,您还需要使用 vid 启用控件.setAttribute('controls',true); 或通过创建自定义控件。

It's possible to add the video element to the DOM using JavaScript in Opera 10.63. The code below, for example, should work (I tested it in Opera 10.61 and Opera 11).

var vid, makevid;
makevid = function(e){
    vid = document.createElement('video');
    vid.src = 'path/to/an/oggtheorafile.ogv';
        vid.setAttribute('autoplay',true);
    document.getElementsByTagName('body')[0].appendChild(vid);
} 
window.addEventListener('load',makevid,false);

You do have to be sure that you're using a compatible video format, though. Opera 10.63 supports Ogg/Theora and WebM. If you're using MPEG4/H.264, the video will not play in Opera.

(EDITED to correct video format support in Opera 10.6x. It does indeed support WebM)

EDIT 2: Note that if you use JavaScript to insert a video element in the DOM, you will also need to enable the controls by using vid.setAttribute('controls',true); or by creating custom controls.

反目相谮 2024-10-13 02:37:53

您可以尝试使用 HTML5 Inner Shiv。我知道它更针对 IE,但可能值得一试。

You could try using the HTML5 Inner Shiv. I know it's more aimed at IE, but it might be worth a shot.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文