Add canvas to a page with javascript

前端 未结 3 664
半阙折子戏
半阙折子戏 2020-12-14 05:50

I am trying to use Javascript in order to add a canvas to one page which originally does not have one. I am trying to do the following:

var canv=document.cre         


        
3条回答
  •  独厮守ぢ
    2020-12-14 06:42

    Use something like Node.appendChild( child ) for adding it to the DOM:

    var canv = document.createElement('canvas');
    canv.id = 'someId';
    
    document.body.appendChild(canv); // adds the canvas to the body element
    document.getElementById('someBox').appendChild(canv); // adds the canvas to #someBox
    

    Or you can use element.innerHTML:

    document.body.innerHTML += ''; // the += means we add this to the inner HTML of body
    document.getElementById('someBox').innerHTML = ''; // replaces the inner HTML of #someBox to a canvas
    

提交回复
热议问题