
将 html 元素动态插入 iframe
本文讨论了如何在 iframe 中动态插入 html 元素以满足将表转换成 excel 的要求。
在提供的问题代码中,从服务器获取的 html 数据形式如下:
| 交易日期 | 总订单数 | 交易总额 | 完成订单数 | 完成总额 | 撤单总订单数 | 撤单总额 |
|---|---|---|---|---|---|---|
| 2017-12-05 | 7414 | 1483.6 | 7414 | 1483.6 | 0 | 0 |
为了将此 html 片段插入到 iframe 中,以下步骤可能有用:
立即学习“前端免费学习笔记(深入)”;
-
创建 iframe 元素:
-
使用 document.getelementbyid("exceliframe").contentwindow.document 访问 iframe 的文档对象:
const iframedocument = document.getelementbyid("exceliframe").contentwindow.document; -
然后,您可以使用 iframedocument.body.innerhtml = htmldata 来将获取的 html 数据插入 iframe 的正文中:
iframeDocument.body.innerHTML = htmlData;
通过这些步骤,动态生成的 html 表格将插入到 iframe 中,用户可以将其另存为 excel 文件以完成导出操作。











