admin管理员组文章数量:1357078
I'm wanting to get an image to display using innerHTML and I just can't seem to get it to work. It works fine with text and I'm trying to get it to display dynamically with other items inside innerHTML. Updated: the image is inside another element in the innerHTML. Image Example:
var div = document.createElement('div');
var image = document.createElement('img');
image.setAttribute('src', "/200/300");/*sample pic*/
div.innerHTML = '<a href="/"><h3>title</h3>'+ image + '</a>';
document.getElementById('block').appendChild(div);/*work fine if appendChild(image) or text inside innerHTML*/
<div id="block">
image
</div>
I'm wanting to get an image to display using innerHTML and I just can't seem to get it to work. It works fine with text and I'm trying to get it to display dynamically with other items inside innerHTML. Updated: the image is inside another element in the innerHTML. Image Example:
var div = document.createElement('div');
var image = document.createElement('img');
image.setAttribute('src', "https://picsum.photos/200/300");/*sample pic*/
div.innerHTML = '<a href="/"><h3>title</h3>'+ image + '</a>';
document.getElementById('block').appendChild(div);/*work fine if appendChild(image) or text inside innerHTML*/
<div id="block">
image
</div>
Share
Improve this question
edited Nov 12, 2018 at 20:08
Victoria Le
asked Nov 12, 2018 at 19:58
Victoria LeVictoria Le
9401 gold badge8 silver badges20 bronze badges
1
-
1
innerHTML
takes a string, not anHTMLElement
– Heretic Monkey Commented Nov 12, 2018 at 20:01
6 Answers
Reset to default 2The remended way of adding an image would be via appendChild()
, however to add an image element in the way you require, you'd need to specify the image in terms of its HTML tag and attributes as html, as follows:
var div = document.createElement('div');
// Define the image tag and html directly and then apply to innerHTML in this way
div.innerHTML = '<img src="' + "https://picsum.photos/200/300" + '" />';
document.getElementById('block').appendChild(div);
<div id="block">
image
</div>
innerHtml is used to parse a string as HTML code.
To attach the image to the DOM element use:
div.appendChild(image);
Use .appendChild()
on the image to make it a child of the div, instead of innerHTML:
var div = document.createElement('div');
var image = document.createElement('img');
image.setAttribute('src', "https://picsum.photos/200/300");/*sample pic*/
div.appendChild(image);
document.getElementById('block').appendChild(div);/*work fine if appendChild(image) or text inside innerHTML*/
<div id="block">
image
</div>
See "innerHTML += ..." vs "appendChild(txtNode)" for the differences in the two
Use this code instead of innerHTML
var div = document.createElement('div');
var image = document.createElement("img");
image.setAttribute('src', "https://picsum.photos/200/300");/*sample pic*/
document.getElementById('block').appendChild(div).appendChild(image);
you need to use appendChild instead of innerHTML:
var div = document.createElement('div');
var image = document.createElement('img');
image.setAttribute('src', "https://picsum.photos/200/300");
div.appendChild(image);
document.getElementById('block').appendChild(div);
<div id="block">
image
</div>
You could change this line
div.innerHTML = image;
To this:
div.innerHTML = image.outerHTML;
You're setting the innerHTML to an object currently instead of to html.
本文标签: javascriptImage doesn39t display inside innerHTMLStack Overflow
版权声明:本文标题:javascript - Image doesn't display inside innerHTML - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744074210a2586440.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论