我想打印一个div的内容,不想在打印预览中显示该div中的某些元素,我试图在@media print中为这些元素赋予noprint类,但没有成功!请帮忙!
这是Javascript
<script type="text/javascript">
function PrintPanel() {
var panel = document.getElementById("content");
var printWindow = window.open('', '', 'height=400,width=800');
printWindow.document.write('<html><head><title>DIV Contents</title>');
printWindow.document.write('</head><body >');
printWindow.document.write(panel.innerHTML);
printWindow.document.write('</body></html>');
printWindow.document.close();
setTimeout(function () {
printWindow.print();
}, 500);
return false;
}
</script>
这是css
<style>
.noprint {
color: red;
}
@media print {
p {
color: green;
}
.noprint {
display: none;
}
}
</style>
这是html
<body>
<div id="content">
<p>show this, in print preview</p>
<div class="noprint">
dont show this, in print preview!
</div>
</div>
<a id="btnPrint" runat="server" Text="Print" onclick="PrintPanel();" style="cursor:pointer;">print</a>
我在JSFIDLE中提供了一个示例:
sample link