Adding images to an HTML document with javascript

后端 未结 6 1968
故里飘歌
故里飘歌 2020-11-30 04:22

I\'ve tried some HTML DOM code from several sites, but it isn\'t working. It isn\'t adding anything. Does anyone have a working example on this?

this.img =          


        
6条回答
  •  孤街浪徒
    2020-11-30 04:43

    You need to use document.getElementById() in line 3.

    If you try this right now in the console:

    var img = document.createElement("img");
    img.src = "http://www.google.com/intl/en_com/images/logo_plain.png";
    var src = document.getElementById("header");
    src.appendChild(img);

    ... you'd get this:

提交回复
热议问题