如何打印指定的div(无需手动禁用页面上的所有其他内容)?
我想要避免一个新的预览对话框,所以用这个内容创建一个新窗口是没有用的。
该页面包含了几个表格,其中一个包含了我想打印的div -表格是用web的视觉样式设计的,不应该显示在打印中。
如何打印指定的div(无需手动禁用页面上的所有其他内容)?
我想要避免一个新的预览对话框,所以用这个内容创建一个新窗口是没有用的。
该页面包含了几个表格,其中一个包含了我想打印的div -表格是用web的视觉样式设计的,不应该显示在打印中。
当前回答
printDiv(divId):在任何页面上打印任何div的通用解决方案。
我有一个类似的问题,但我想(a)能够打印整个页面,或(b)打印几个特定区域中的任何一个。由于上面的很多内容,我的解决方案允许您指定要打印的任何div对象。
此解决方案的关键是向打印媒体样式表添加适当的规则,以便打印所请求的div(及其内容)。
首先,创建所需的打印css来屏蔽所有内容(但没有允许您想要打印的元素的特定规则)。
<style type="text/css" media="print">
body {visibility:hidden; }
.noprintarea {visibility:hidden; display:none}
.noprintcontent { visibility:hidden; }
.print { visibility:visible; display:block; }
</style>
注意,我添加了新的类规则:
Noprintarea允许您禁止打印div中的元素——包括内容和块。 Noprintcontent允许您抑制div中元素的打印—内容被抑制,但分配的区域为空。 打印让你有项目显示在打印版本,但 不在屏幕上。它们通常会有“display:none”作为屏幕样式。
然后插入三个JavaScript函数。第一个选项只是打开和关闭打印媒体样式表。
function disableSheet(thisSheet,setDisabled)
{ document.styleSheets[thisSheet].disabled=setDisabled; }
第二个做真正的工作,第三个事后清理。第二个(printDiv)激活打印媒体样式表,然后追加一个新规则以允许所需的div打印,发出打印,然后在最后的清理之前添加一个延迟(否则可以在打印实际完成之前重置样式)。
function printDiv(divId)
{
// Enable the print CSS: (this temporarily disables being able to print the whole page)
disableSheet(0,false);
// Get the print style sheet and add a new rule for this div
var sheetObj=document.styleSheets[0];
var showDivCSS="visibility:visible;display:block;position:absolute;top:30px;left:30px;";
if (sheetObj.rules) { sheetObj.addRule("#"+divId,showDivCSS); }
else { sheetObj.insertRule("#"+divId+"{"+showDivCSS+"}",sheetObj.cssRules.length); }
print();
// need a brief delay or the whole page will print
setTimeout("printDivRestore()",100);
}
最后一个函数删除添加的规则,并再次将打印样式设置为禁用,以便可以打印整个页面。
function printDivRestore()
{
// remove the div-specific rule
var sheetObj=document.styleSheets[0];
if (sheetObj.rules) { sheetObj.removeRule(sheetObj.rules.length-1); }
else { sheetObj.deleteRule(sheetObj.cssRules.length-1); }
// and re-enable whole page printing
disableSheet(0,true);
}
唯一要做的另一件事是在onload处理中添加一行,这样打印样式最初是禁用的,从而允许整页打印。
<body onLoad='disableSheet(0,true)'>
然后,在文档的任何地方,你都可以打印一个div。只要从按钮或其他地方发出printDiv("thedivid")即可。
这种方法的一大优点是它提供了从页面中打印选定内容的通用解决方案。它还允许对打印的元素使用现有样式——包括包含的div。
注意:在我的实现中,这必须是第一个样式表。将表引用(0)更改为适当的表号,如果需要将其置于表序列的后面。
其他回答
您是否可以使用打印样式表,并使用CSS来排列想要打印的内容?阅读这篇文章获取更多的建议。
我来这个派对已经很晚了,但我想提出另一种方法。我写了一个叫做PrintElements的小JavaScript模块,用于动态打印网页的各个部分。
It works by iterating through selected node elements, and for each node, it traverses up the DOM tree until the BODY element. At each level, including the initial one (which is the to-be-printed node’s level), it attaches a marker class (pe-preserve-print) to the current node. Then attaches another marker class (pe-no-print) to all siblings of the current node, but only if there is no pe-preserve-print class on them. As a third act, it also attaches another class to preserved ancestor elements pe-preserve-ancestor.
一个非常简单的补充只打印的css将隐藏和显示各自的元素。这种方法的一些好处是保留了所有的样式,它不需要打开一个新窗口,不需要移动大量的DOM元素,而且通常它对原始文档是非侵入性的。
请参阅演示,或阅读相关文章以获得更多详细信息。
嗯……使用样式表的类型进行打印…例如:
<link rel="stylesheet" type="text/css" href="print.css" media="print" />
print.css:
div { display: none; }
#yourdiv { display: block; }
桑德罗的方法很有效。
我对它进行了调整,以允许多个printMe链接,特别是在选项卡页面和扩展文本中使用。
function processPrint(printMe){<——在这里调用一个变量
var printReadyElem = document.getElementById(printMe);<——删除了printMe周围的引号以请求一个变量
< a href = " javascript:无效(processPrint (' divID '));>Print</a> <——将要打印的div ID传递到函数上,将printMe变量转换为div ID。需要单引号
我尝试了提供的许多解决方案。没有一个是完美的。它们要么丢失CSS,要么不能在所有浏览器中工作。我找到了一个完美而简单的解决方案,既不丢失CSS,也适用于所有浏览器。
Html
<div class="row" id="print-div">
Your html
</div>
打印稿
let popupWin = window.open('', '_blank', 'width=1080,height=595');
let printContents = document.getElementById("print-div").innerHTML;
let printHead = document.head.innerHTML;
popupWin.document
.write(`<html>
${printHead}
<body onload="window.print();">${printContents}</body></html>`);
popupWin.document.close();