0

JavaScript

<script type="text/javascript">
        function printDiv() {

 var printContent1 = document.getElementById('divMail1');
 var printContent2 = document.getElementById('divMail2');

            var newWin = window.open('', 'PrintWindow', 'width=800,height=530,top=10,left=100,scrollbars=1,resizable=yes');
            newWin.document.open();
            newWin.document.write('<html/><body  onload="window.print()">' + printContent1.innerHTML + '</body></html>');
            newWin.document.close();
        }

</script>

HTML

  <div id="divMail1">
        <div>
            <img src="image1.jpg"/>
        </div>
     <div>
            <img src="image2.jpg"/>
        </div>


    <div id="divMail2">
        <div>
            <img src="image3.jpg"/>
        </div>
     <div>
            <img src="image4.jpg"/>
        </div>
    </div>
<input type="button" value="Print" onclick="printDiv()"/>

嗨,我想在打印预览中显示一个 div(divMail1) 并打印另一个 div(divMail2)。可能吗。请帮我。

4

3 回答 3

2

只用 CSS 做

<head>
        <style type="text/css">

        #printable { display: none; }

        @media print
        {
            #non-printable { display: none; }
            #printable { display: block; }
        }
        </style>
    </head>
    <body>
        <div id="non-printable">
            Your normal page contents
        </div>

        <div id="printable">
            Printer version
        </div>
    </body>
于 2013-10-26T07:21:35.517 回答
1

您可以print.css通过添加一个media="print". 因此,在您的主 CSS 中,您需要添加以下内容:

.print-only {display: none;}
.do-not-print {display: block;}

并且在print.css

.print-only {display: block;}
.do-not-print {display: none;}

在您的情况下,您的主要 CSS 中的代码将是:

#divMail1 {display: block;}
#divMail2 {display: none;}

并且在print.css

#divMail1 {display: none;}
#divMail2 {display: block;}
于 2013-10-26T07:20:50.027 回答
0

改用 css

#divMail1 {
  display: block;
}

#divMail2 {
  display: none;
}
@media print {
  #divMail1 {
    display: none;
  }

  #divMail2 {
    display: block;
  }
}
于 2013-10-26T07:23:26.347 回答