window.print() CSS 未在打印预览中加载

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16365238/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 22:43:54  来源:igfitidea点击:

window.print() CSS not loaded in print preview

javascriptcssinternet-explorerprinting

提问by Balaji Krishnan

I need to provide option to print a part of the webpage. This was implemented using the javascript window.print()answered by: Bill Paetzke. The pop up window opens up (this contains CSS style) and the print dialog also opens. But the CSS style does not show up in the print. Tried @media =print but that also does not work. CSS style mainly consists of background color. One option that I got was to replace the background color with an image of 1px * 1px and repeat it. Is there any other way?

我需要提供打印部分网页的选项。这是使用 javascript window.print()实现的,由 Bill Paetzke 回答。弹出窗口打开(这包含 CSS 样式)并且打印对话框也打开。但是 CSS 样式没有显示在打印中。试过@media =print 但这也不起作用。CSS 样式主要由背景颜色组成。我得到的一个选择是用 1px * 1px 的图像替换背景颜色并重复它。有没有其他办法?

Browser: IE7 Code:

浏览器:IE7 代码:

<html>
<head>
<style type="text/css" media="print,screen">
.hideMe{
display:block;
}

.PrintClass {
display:block;

}
.NoPrintClass{
display:block;
}
</style>
<script type="text/javascript"
    src="http://jqueryjs.googlecode.com/files/jquery-1.3.1.min.js"> </script>
<script type="text/javascript">

    function PrintElem(elem)
    {
        Popup($('<div/>').append($(elem).clone()).html());
    }

    function Popup(data) 
    {

        var mywindow;       
        mywindow = window.open('', 'mydiv','height=400,width=600,scrollbars=yes','');            
        mywindow.document.write('<html><head><title>my div</title>');
        mywindow.document.write('<style type="text/css" media="print,screen">.hideMe{display:none;}.NoPrintClass{display:none;}</style>');               
        mywindow.document.write('</head><body>');
        mywindow.document.write('drop down selected value in parent: '+mywindow.opener.document.getElementById('testSelect').options[mywindow.opener.document.getElementById('testSelect').selectedIndex].text+'<br/>');
        mywindow.document.write('contentStarts<br/>');
        mywindow.document.write('  using jquery:  '+data);
        mywindow.document.write(' using javascript: '+mywindow.opener.document.getElementById('mydiv').innerHTML);
        mywindow.document.write('<br/>contentEnds'); 
        mywindow.document.write('<br/>');                                     
        mywindow.document.write('</body></html>');        
        mywindow.document.focus();
        mywindow.document.close();      
        mywindow.print();             
        return true;
    }

   </script>
</head>


<body>

This is not be printed<br/>
<div id="mydiv">This content is to be printed<br/>
<div class="hideMe"><select id="testSelect">
    <option>1</option>
    <option>2</option>
    <option>3</option>
</select>
</div>
<br/>
This will also be printed 
<br/>print  bgcolor
<div style="background:red;" class="PrintClass">print</div><br/>
<div style="background:green;" class="PrintClass">print</div><br/>
<div style="background:red;" class="NoPrintClass">not to print</div><br/>
<div style="background:green;" class="NoPrintClass">not to print</div><br/>
<div style="background:red;" class="PrintClass">print</div><br/>
<div style="background:green;" class="PrintClass">print</div><br/>
<br/> print image
<div style="background-image:url('red.png');background-repeat:repeat;" class="PrintClass">print</div><br/>
<div style="background-image:url('green.png');background-repeat:repeat;" class="PrintClass">print</div><br/>
<div style="background-image:url('red.png');background-repeat:repeat;" class="NoPrintClass">not to print</div><br/>
<div style="background-image:url('green.png');background-repeat:repeat;" class="NoPrintClass">not to print</div><br/>
<div style="background-image:url('red.png');background-repeat:repeat;" class="PrintClass">print</div><br/>
<div style="background-image:url('green.png');background-repeat:repeat;" class="PrintClass">print</div><br/>
<text>some text
<div style="position:relative;">
<img src="green.png" width="100px" height="100px" value="abcd">dfads
<div style="postion:absolute">wpeoriu</div>
</div>
</img>
</text>

</div>

<div>This will not be printed.</div>
<br/>
<div id="anotherdiv">Nor will this.</div>

<input type="button" value="Print Div" onclick="PrintElem('mydiv')" />



</body>

</html>

回答by NullPointerException

Because you are writing document with the html without any styles ids That means it is only writing the content of div without div around it.

因为您正在使用没有任何样式 ID 的 html 编写文档这意味着它只编写 div 的内容而没有 div 围绕它。

use this to get the whole div

使用它来获取整个 div

 $('<div/>').append($(elem).clone()).html();

See this fiddle

看到这个小提琴

Html

html

  <div id="mydiv">
    This will be printed. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque a quam at nibh adipiscing interdum. Nulla vitae accumsan ante. 
  </div>
    <div>
   This will not be printed.
  </div>

<div id="anotherdiv">
    Nor will this.
</div>

<input type="button" value="Print Div" onclick="PrintElem('#mydiv')" />

JavaScript

JavaScript

function PrintElem(elem)
{
      Popup($('<div/>').append($(elem).clone()).html());
}

function Popup(data) 
{
    var mywindow = window.open('', 'my div', 'height=400,width=600');
    mywindow.document.write('<html><head><title>my div</title>');
     mywindow.document.write('<link rel="stylesheet" href="http://www.dynamicdrive.com/ddincludes/mainstyle.css" type="text/css" />');
    mywindow.document.write('</head><body >');
    mywindow.document.write(data);
    mywindow.document.write('</body></html>');

    mywindow.print();
  //  mywindow.close();

    return true;
}

回答by MariaC

I found thispage helpful. Take a look.

我发现这个页面很有帮助。看一看。

Print & print preview with print css works great. I did it in opencart product page. Now I try to print it pdf format, if you have some clue, it would be great.

使用打印 css 打印和打印预览效果很好。我是在 opencart 产品页面上做的。现在我尝试将其打印为 pdf 格式,如果您有一些线索,那就太好了。