iframe如何获取antd的样式

发布于 2022-09-13 00:58:41 字数 1318 浏览 5 评论 0

需求场景

打印

iframe打印方案

const handlePrint = useDebounceFn(() => {

  // 判断iframe是否存在,不存在则创建iframe

  let iframe = document.getElementById("print-iframe");

  if (!iframe) {

    var el = document.getElementById("table");

    iframe = document.createElement('IFRAME');

    var doc = null;

    iframe.setAttribute("id", "print-iframe");

    iframe.setAttribute('style', 'position:absolute;width:0px;height:0px;left:-500px;top:-500px;');

    document.body.appendChild(iframe);

    doc = iframe.contentWindow.document;

    //这里可以自定义样式

    doc.write('<style media="print">@page {size: auto;margin: 0mm;}</style>'); // 解决出现页眉页脚和路径的问题

    doc.write('<div>' + el.innerHTML + '</div>');

    doc.close();

    iframe.contentWindow.focus();

  }

  setTimeout(function () { iframe.contentWindow.print(); }, 50)  // 解决第一次样式不生效的问题

  if (navigator.userAgent.indexOf("MSIE") > 0) {

    document.body.removeChild(iframe);

  }

}, {})
render(){
return <Table datasource={data} columns={columns} id="table"  />
}

问题

预览出的iframe展示的table没有antd的样式,请问如何才能让iframe拿到antd的样式。

我试着这样写,但是不正确

doc.write(`<LINK rel="stylesheet" type="text/css" href="${require('antd/dist/antd.css')}">`);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文