print打印頁生成及排版樣式更改的技巧
阿新 • • 發佈:2017-09-07
eight 更改 cti 實現 cst close tdi 排版 element
<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>
print打印頁生成及排版樣式更改的技巧