Print lt;div id=quot;printareaquot;gt;lt;/divgt; only?(打印 lt;div id=printareagt;lt;/divgt;只要?)
问题描述
如何打印指定的 div(无需手动禁用页面上的所有其他内容)?
How do I print the indicated div (without manually disabling all other content on the page)?
我想避免一个新的预览对话框,所以用这个内容创建一个新窗口是没有用的.
I want to avoid a new preview dialog, so creating a new window with this content is not useful.
该页面包含几个表格,其中一个包含我要打印的 div - 该表格的样式是 Web 的视觉样式,不应在打印中显示.
The page contains a couple of tables, one of them contains the div I want to print - the table is styled with visual styles for the web, that should not show in print.
推荐答案
这是一个通用的解决方案,使用 CSS only,我已经验证可以工作.
Here is a general solution, using CSS only, which I have verified to work.
@media print {
body * {
visibility: hidden;
}
#section-to-print, #section-to-print * {
visibility: visible;
}
#section-to-print {
position: absolute;
left: 0;
top: 0;
}
}
替代方法不是很好.使用 display
很棘手,因为如果任何元素有 display:none
那么它的后代都不会显示.要使用它,您必须更改页面结构.
Alternative approaches aren't so good. Using display
is tricky because if any element has display:none
then none of its descendants will display either. To use it, you have to change the structure of your page.
使用 visibility
效果更好,因为您可以打开后代的可见性.不可见的元素仍然会影响布局,所以我将 section-to-print
移动到左上角以便正确打印.
Using visibility
works better since you can turn on visibility for descendants. The invisible elements still affect the layout though, so I move section-to-print
to the top left so it prints properly.
这篇关于打印 <div id="printarea"></div>只要?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:打印 <div id="printarea"></div>只要?


基础教程推荐
- Bokeh Div文本对齐 2022-01-01
- Fabric JS绘制具有活动形状的多边形 2022-01-01
- 即使用户允许,Gmail 也会隐藏外部电子邮件图片 2022-01-01
- Bootstrap 模态出现在背景下 2022-01-01
- fetch 是否支持原生多文件上传? 2022-01-01
- 检查 HTML5 拖放文件类型 2022-01-01
- npm start 错误与 create-react-app 2022-01-01
- 在 contenteditable 中精确拖放 2022-01-01
- 如何添加到目前为止的天数? 2022-01-01
- 原生拖动事件后如何获取 mouseup 事件? 2022-01-01