繁体   English   中英

如何在js中的iframe中插入img标签?

[英]How to insert an img tag in an iframe in js?

我的文档中有一个 img 标签。 我想将它插入到我创建的 iframe 标签中。 这是我的代码:

 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>

我可以将我的 img 标签插入到 frame 标签中。 但是,图像本身不会在 iframe 中显示。 我认为这可能是因为 img 标签在 iframe 的#document之外(查看 DOM 的检查按钮)。 那么,我如何将它正确地放入 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM