[英]How to insert an img tag in an iframe in js?
I have an img tag in my document.我的文档中有一个 img 标签。 I want to insert it into an iframe tag I created.
我想将它插入到我创建的 iframe 标签中。 Here is my code:
这是我的代码:
var ARiframe = document.createElement("iframe"); const imgInDom = document.getElementById("apple_img"); imgInDom.parentNode.insertBefore(ARiframe, imgInDom); ARiframe.appendChild(imgInDom);
<html> <body> <div class="fruit"> <img id="apple_img" src="https://parade.com/.image/t_share/MTkwNTgxNDY1MzcxMTkxMTY0/different-types-of-apples-jpg.jpg" width="100" height="100"> </div> </body> </html>
I am able to insert my img tag into the frame tag.我可以将我的 img 标签插入到 frame 标签中。 However, the image itself does not display when in the iframe.
但是,图像本身不会在 iframe 中显示。 I think the reason for this could be that the img tag is outside the
#document
for the iframe (look at the inspect button for the DOM).我认为这可能是因为 img 标签在 iframe 的
#document
之外(查看 DOM 的检查按钮)。 So, how would I put it in the iframe correctly so that the image displays?那么,我如何将它正确地放入 iframe 中以便图像显示呢?
Append it to the contentDocument.body
of the <iframe>
.将其附加到
<iframe>
的contentDocument.body
。
var ARiframe = document.createElement("iframe");
const imgInDom = document.getElementById("apple_img");
imgInDom.parentNode.insertBefore(ARiframe, imgInDom);
ARiframe.contentDocument.body.appendChild(imgInDom);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.