1. 程式人生 > >js列印頁面指定區域內容

js列印頁面指定區域內容

2種方式: 一種只支援IE瀏覽器 

        另一種支援各類瀏覽器 

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK" />
<title>列印設定</title>
<script language="JavaScript" type="text/javascript">
//<![CDATA[
    var hkey_root,hkey_path,hkey_key;
    hkey_root="HKEY_CURRENT_USER";
    hkey_path="\\Software\\Microsoft\\Internet Explorer\\PageSetup\\";
    //配置網頁列印的頁首頁尾為空 
  //wb.ExecWB(1,1) 開啟 
  //wb.ExecWB(2,1) 關閉現在所有的IE視窗,並開啟一個新視窗 
  //wb.ExecWB(4,1) 儲存網頁 
  //wb.ExecWB(6,1) 列印 
 // wb.ExecWB(7,1) 列印預覽 
 // wb.ExecWB(8,1) 列印頁面設定 
 // wb.ExecWB(10,1) 檢視頁面屬性 
  //wb.ExecWB(15,1) 好像是撤銷,有待確認 
 // wb.ExecWB(17,1) 全選 
 // wb.ExecWB(22,1) 重新整理 
 // wb.ExecWB(45,1) 關閉窗體無提示
    function pagesetup_null(){  
        try{
            var RegWsh = new ActiveXObject("WScript.Shell");          
            hkey_key="header";          
            RegWsh.RegWrite(hkey_root+hkey_path+hkey_key,"");
            hkey_key="footer";
            RegWsh.RegWrite(hkey_root+hkey_path+hkey_key,"");
            //&b 第&p頁/共&P頁 &b
        }catch(e){}
    }
    //配置網頁列印的頁首頁尾為預設值
    function pagesetup_default(){
        try{
            var RegWsh = new ActiveXObject("WScript.Shell");
            hkey_key="header";
            RegWsh.RegWrite(hkey_root+hkey_path+hkey_key,"&w&b頁碼,&p/&P")
            hkey_key="footer";
            RegWsh.RegWrite(hkey_root+hkey_path+hkey_key,"&u&b&d");
        }catch(e){}
    }  
    
    //列印頁面預覽
    function printpreview(){
        pagesetup_null();
        //wb.printing.header = "居左顯示&b居中顯示&b居右顯示頁碼,第&p頁/共&P頁";
        //wb.printing.footer = "居左顯示&b居中顯示&b居右顯示頁碼,第&p頁/共&P頁";
        try{
            wb.execwb(7,1);
        }catch(e){
            alert("您的瀏覽器不支援此功能,請選擇'檔案'->'列印預覽'");
        }
    }
    //列印
    function prints(){
        pagesetup_null();
        //wb.printing.header = "居左顯示&b居中顯示&b居右顯示頁碼,第&p頁/共&P頁";
        //wb.printing.footer = "居左顯示&b居中顯示&b居右顯示頁碼,第&p頁/共&P頁";
        try{
            wb.execwb(6,1);
        }catch(e){
            alert("您的瀏覽器不支援此功能");
        }
    }
  //]]>
  
   //列印選區內容  包含在 對應標識裡面的內容為列印內容
    function doPrint() {
        bdhtml=window.document.body.innerHTML; //獲取當前頁的html程式碼
        sprnstr="<!--startprint-->"; //設定列印開始區域 
        eprnstr="<!--endprint-->";  //設定列印結束區域 
        prnhtml=bdhtml.substr(bdhtml.indexOf(sprnstr)+17);  //從開始程式碼向後取html 
        prnhtml=prnhtml.substring(0,prnhtml.indexOf(eprnstr));  //從結束程式碼向前取html 
        window.document.body.innerHTML=prnhtml;
        window.print();
		window.document.body.innerHTML=bdhtml;    //還原頁面
    }
  </script>
<style type="text/css">
body {
    margin-top: 0px;
    margin-right: auto;
    margin-bottom: 0px;
    margin-left: auto;
    width: 980px;
}
@media print {
	.Noprint {
		display: none;
	}
}
</style>
</head>
 
<body>
<div id="bankwrap">
  <!--這種方式 適用於 IE瀏覽器-->	
  <object classid="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2" id="wb" width="0" height="0">
  </object>
  <div>
    <p class="Noprint">
	<span style="cursor:pointer; color:#0000FF" onclick="printpreview();">列印預覽</span> 
	<span style="cursor:pointer; color:#0000FF" onclick="prints();" class="Noprint">列印</span>
	<span style="cursor:pointer; color:#0000FF" onclick="doPrint();" >列印指定區域頁面</span>
	</p>
  </div>
  <h1>列印的內容(上面一行字只顯示,不列印)</h1>
  <div>撒大事大是打算打誰說的</div>
  <!--startprint-->
	<div class="banktitle"> 2016-10-01放假安排:<br />
	<img src="chishi.gif"/><br/>
	連續放假2禮拜。<br />
   </div>
	<!--endprint-->
</div>
</body>
</html>