print打印页生成及排版样式更改的技巧

<script>
function myPrint(obj){
//注:可以在打印页生成前前通过js更改样式,来实现排版,打印页生成后通过setTimeout延迟将页面样式变回去
//打开一个新窗口newWindow
var newWindow=window.open("打印窗口","_blank",'height=300,widht=400');
//要打印的div的内容
var docStr = obj.innerHTML;
//打印内容写入newWindow文档
newWindow.document.write(docStr);
//关闭文档
newWindow.document.close();
//调用打印机
newWindow.print();

//注:打印页生成后可以通过setTimeout延迟将页面样式变回去
//关闭newWindow页面
newWindow.close();

}

// myPrint(document.getElementById('printDivID'));//调用方法
</script>
<div id="print">
<hr />
打印演示区域,点击打印后会在新窗口加载这里的内容!
<hr />
</div>
<button onclick="myPrint(document.getElementById('print'))">打 印</button>

posted @ 2017-09-07 14:03  野望之风  阅读(714)  评论(0编辑  收藏  举报