动态创建HTML5视频元素,而不会在页面中显示 [英] Dynamically create a HTML5 video element without it being shown in the page
问题描述
是否可以动态创建HTML5视频元素,以便我可以通过API访问该元素,如 document.getElementById
或Name,但它可能不会显示在网页中
Is it possible to dynamically create a HTML5 video element so that I can access the element by API's like document.getElementById
or Name but it may not show up in the webpage.
像 div.hide()
或这方面的某些东西?
Something like div.hide()
or something in that direction ?
推荐答案
您可以尝试
var video = document.createElement('video');
video.src = 'urlToVideo.ogg';
video.autoPlay = true;
您还可以使用 canPlayType
方法检查浏览器是否支持要设置源代码之前要使用的视频格式
you can also use the canPlayType
method to check if the browser supports the video format you want to use before setting source
if (video.canPlayType('video/ogg').length > 0) {
/* set some video source */
}
该方法返回也许
或或许
根据浏览器。如果空字符串意味着它无法播放。
The method returns maybe
or perhaps
depending on browser. If empty string it means it can't play it.
现在,您可以使用API使用视频
。只需在全球范围内存储。您可以稍后将其插入到DOM中。希望这有帮助。
You can now use the video
using the API. Just store it globally. You can later insert it into the DOM. Hope this helps.
这篇关于动态创建HTML5视频元素,而不会在页面中显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!