【问题标题】:the Css is not read in the print code打印代码中未读取 Css
【发布时间】:2016-03-28 09:26:51
【问题描述】:

我试过这个脚本,但是打印时样式读取不正确,谁能帮我找出问题所在?

 <script type="text/javascript">
    $(function () {
        $("#btnPrint").click(function () {
            var contents = $("#dvContents").html();
            var frame1 = $('<iframe />');
            frame1[0].name = "frame1";
            frame1.css({ "position": "absolute", "top": "-1000000px" });
            $("body").append(frame1);
            var frameDoc = frame1[0].contentWindow ? frame1[0].contentWindow : frame1[0].contentDocument.document ? frame1[0].contentDocument.document : frame1[0].contentDocument;
            frameDoc.document.open();
            //Create a new HTML document.
            frameDoc.document.write('<html><head><title>DIV Contents</title>');
            frameDoc.document.write('</head><body>');
            //Append the external CSS file.
            frameDoc.document.write('<link href="style.css" rel="stylesheet" type="text/css" />');
            //frameDoc.document.write('<link href="bootstrap.min.css" rel="stylesheet" type="text/css" />');
            //Append the DIV contents.
            frameDoc.document.write(contents);
            frameDoc.document.write('</body></html>');
            frameDoc.document.close();
            setTimeout(function () {
                window.frames["frame1"].focus();
                window.frames["frame1"].print();
                frame1.remove();
            }, 500);
        });
    });
</script>

【问题讨论】:

  • 您的样式表是否包含@media print?

标签: javascript jquery css twitter-bootstrap printing


【解决方案1】:

问题可能与 css 文件路径有关。

这样按照根路径写.....

<link rel="stylesheet" href="../css/style.css" />

【讨论】:

  • 路径是正确的,它与我用于主页的路径相同问题出在打印页面中,我想用它的 UI 打印一个特定的 div
猜你喜欢
  • 2020-11-05
  • 1970-01-01
  • 2018-03-16
  • 2017-05-12
  • 2017-09-12
  • 1970-01-01
  • 1970-01-01
  • 2010-11-26
相关资源
最近更新 更多