標籤:style blog http color io ar 2014 div sp
所有瀏覽器都可以
<html><head title=""> <title>測試列印</title> <style media="print"> /*只應用於列印的樣式*/ .noprint { display: none; } </style> <script>
//通過標籤控制 function printDiv1(oper) { bdhtml = window.document.body.innerHTML;//擷取當前頁的html代碼 sprnstr = "<!--startprint" + oper + "-->";//設定列印開始地區 eprnstr = "<!--endprint" + oper + "-->";//設定列印結束地區 prnhtml = bdhtml.substring(bdhtml.indexOf(sprnstr) + 18); //從開始代碼向後取html prnhtml = prnhtml.substring(0, prnhtml.indexOf(eprnstr));//從結束代碼向前取html window.document.body.innerHTML = prnhtml; window.print();
window.document.body.innerHTML = bdhtml; } //通過樣式控制
function printDiv2() { window.print(); } </script></head><body> <!--startprint1--> <div>需要列印的(如需列印背景: 在Internet選項--進階--在“列印”一項選上“列印背景顏色和圖象”)</div> <!--endprint1--> <div class="noprint"> <div>不需要列印的</div> <input type="button" value="列印(innerHTML法)" onclick="printDiv1(1);" /> <input type="button" value="列印(樣式法)" onclick="printDiv2();" /> </div></body></html>
如何列印背景:
IE: 在Internet選項--進階--在“列印”一項選上“列印背景顏色和圖象”
或者新版的IE:
Firefox:
Chrome:
JS局部列印兩種方法