js頁面打印並去掉上方日期和下方網頁地址

/*這裏主要是去除頁眉和頁腳*/
 <style type="text/css">
 @page{
            margin-top: 1mm;
            margin-bottom: 1mm;
      }
 </style>
 /*調用瀏覽器的打印方法*/
 <script language="javascript">
        function preview(oper)
        {

            if (oper < 10){
                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;
            } else {
                window.print();
            }
        }
    </script>

下面是頁面html中加入的代碼

 <!--startprint1-->
            <!--打印內容開始-->
            <b>這裏隨便寫</b>
             <!--打印內容結束-->
 <!--endprint1-->
  <input type=button name='button_export' title='打印1' onclick=preview(1) value=打印1>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章