Dynamically create a HTML5 video element without it being shown in the page

前端 未结 3 793
感动是毒
感动是毒 2021-02-05 07:23

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

3条回答
  •  甜味超标
    2021-02-05 07:41

    Updated (and simplest) way to achieve this (since Google searches are leading here):

    var x = document.createElement("VIDEO");
    
    if (x.canPlayType("video/mp4")) {
        x.setAttribute("src","movie.mp4");
    } else {
        x.setAttribute("src","movie.ogg");
    }
    
    x.setAttribute("width", "320");
    x.setAttribute("height", "240");
    x.setAttribute("controls", "controls");
    document.body.appendChild(x);
    

提交回复
热议问题