且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何打印HTML页面的一部分?

更新时间:2023-12-01 23:02:40

您应该为打印介质使用单独的CSS,这样您可以在打印页面时隐藏/显示部分页面。

You should use a separate css for the print media. This allows you to hide/show portions of the page when it gets printed.

html:

<div class="dont-print-that">
   blah
</div>
print this!

包括:

<link rel="stylesheet" type="text/css" media="print" href="print.css" />

print.css

print.css

.dont-print-that{display:none;}

一个只有您要打印的内容的新窗口。您可以在弹出式窗口或iframe中执行此操作。我个人认为CSS解决方案更加优雅,但这取决于你。

The other solution is to open a new window with only the content you want to print. You could either do that in a popup or an iframe. Personally I find the CSS solution more elegant, but that's up to you.