-
陪伴而非守候
在Chrome上测试的早期版本略有变化function PrintElem(elem){
var mywindow = window.open('', 'PRINT', 'height=400,width=600');
mywindow.document.write('<html><head><title>' + document.title + '</title>');
mywindow.document.write('</head><body >');
mywindow.document.write('<h1>' + document.title + '</h1>');
mywindow.document.write(document.getElementById(elem).innerHTML);
mywindow.document.write('</body></html>');
mywindow.document.close(); // necessary for IE >= 10
mywindow.focus(); // necessary for IE >= 10*/
mywindow.print();
mywindow.close();
return true;}
-
扬帆大鱼
我认为有更好的解决办法。让div覆盖整个文档,但只在打印时:@media print {
.myDivToPrint {
background-color: white;
height: 100%;
width: 100%;
position: fixed;
top: 0;
left: 0;
margin: 0;
padding: 15px;
font-size: 14px;
line-height: 18px;
}}
-
翻过高山走不出你
如果使用jQuery,可以使用我的printElement插件。有个样本这里,以及有关插件的更多信息。这里.它的用法相当严格,只需使用jQuery选择器获取一个元素并打印出来:$("myDiv").printElement();希望能帮上忙!