JS列印功能

來源:互聯網
上載者:User

1、window.print方式:
jsp頁面 列印按鈕:     
 <input type="button" value="列印" onclick="print();">
js 中:     
 function print(){
     window.print();
style樣式中,設定隱藏按鈕列印:
<style>  
     @media   print   {  
      .noprint{display:none}  
      }  
 </style>
2、WebBrowser控制項方式
WebBrowser是IE內建的瀏覽器控制項,無需使用者下載.
一、WebBrowser控制項
  <object ID='WebBrowser' WIDTH=0 HEIGHT=0 CLASSID='CLSID:8856F961-340A-11D0-A96B-00C04FD705A2'></object>
二、WebBrowder控制項的方法
//列印
WebBrowser1.ExecWB(6,1);
//列印設定
WebBrowser1.ExecWB(8,1);
//預覽列印
WebBrowser1.ExecWB(7,1);
關於這個組件還有其他的用法,列舉如下:
WebBrowser.ExecWB(1,1) 開啟
Web.ExecWB(2,1) 關閉現在所有的IE視窗,並開啟一個新視窗
Web.ExecWB(4,1) 儲存網頁
Web.ExecWB(6,1) 列印
Web.ExecWB(7,1) 預覽列印
Web.ExecWB(8,1) 列印版面設定
Web.ExecWB(10,1) 查看頁面屬性
Web.ExecWB(15,1) 好像是撤銷,有待確認
Web.ExecWB(17,1) 全選
Web.ExecWB(22,1) 重新整理
Web.ExecWB(45,1) 關閉表單無提示
但是列印是會把整個頁面都列印出來的,頁面裡面有什麼東西就列印出來,我們有時候只需要列印資料表格,這時我們就要寫一個樣式了:把不想列印的部份隱藏起來:
樣式內容:
<style type="text/css" media=print>
.noprint...{display : none }
</style>
然後使用樣式就可以:
<p class="noprint">不需要列印的地方</p>
代碼如下:
<script language="javascript">
function printsetup()...{
// 列印版面設定
wb.execwb(8,1);
function printpreview()...{
// 列印頁面預覽
wb.execwb(7,1);
function printit()
...{
if (confirm('確定列印嗎?')) ...{
wb.execwb(6,6)
</script>
<OBJECT classid="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2" height=0 id=wb name=wb width=0></OBJECT>
<input type=button name=button_print value="列印" class="noprint" onclick="javascript:printit()">
<input type=button name=button_setup value="列印版面設定" class="noprint" onclick="javascript:printsetup();">
<input type=button name=button_show value="預覽列印" class="noprint" onclick="javascript:printpreview();">
3、去除頁首和頁尾 <HTML><HEAD><TITLE>web列印去掉頁首頁尾,以及不想列印出的頁面元素</TITLE>   <META http-equiv=Content-Type content="text/html; charset=gb2312">   <SCRIPT language=javascript>   function printpr() //預覽函數   {   document.all("qingkongyema").click();//列印之前去掉頁首,頁尾   document.all("dayinDiv").style.display="none"; //列印之前先隱藏不想列印輸出的元素(此例中隱藏“列印”和“預覽列印”兩個按鈕)   var OLECMDID = 7;   var PROMPT = 1;   var WebBrowser = '<OBJECT ID="WebBrowser1" WIDTH=0 HEIGHT=0 CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>';   document.body.insertAdjacentHTML('beforeEnd', WebBrowser);   WebBrowser1.ExecWB(OLECMDID, PROMPT);   WebBrowser1.outerHTML = "";   document.all("dayinDiv").style.display="";//列印之後將該元素顯示出來(顯示出“列印”和“預覽列印”兩個按鈕,方便別人下次列印)   }      function printTure() //列印函數   {   document.all('qingkongyema').click();//同上   document.all("dayinDiv").style.display="none";//同上   window.print();   document.all("dayinDiv").style.display="";   }   function doPage()   {   layLoading.style.display = "none";//同上   }            </SCRIPT>            <script language="VBScript">   dim hkey_root,hkey_path,hkey_key   hkey_root="HKEY_CURRENT_USER"   hkey_path="\Software\Microsoft\Internet Explorer\PageSetup"   '//設定網頁列印的頁首頁尾為空白   function pagesetup_null()   on error resume next   Set RegWsh = CreateObject("WScript.Shell")   hkey_key="\header"   RegWsh.RegWrite hkey_root+hkey_path+hkey_key,""   hkey_key="\footer"   RegWsh.RegWrite hkey_root+hkey_path+hkey_key,""   end function   '//設定網頁列印的頁首頁尾為預設值   function pagesetup_default()   on error resume next   Set RegWsh = CreateObject("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"   end function   </script>            </HEAD>   <BODY background="images/background_01.gif" leftMargin=0   topMargin=0 rightMargin=0 bottomMargin=0 style="BACKGROUND-POSITION: center 50%">            <DIV align=center>      你希望列印的內容..........   </DIV>            <DIV align="center" id="dayinDiv" name="dayinDiv"><input type="button" class="tab" value="列印" onclick="printTure();">     <input   type="button" class="tab" value="預覽列印" onclick="printpr();">   <input type="hidden" name="qingkongyema" id="qingkongyema" class="tab" value="清空頁碼" onclick="pagesetup_null()">     <input type="hidden" class="tab" value="恢複頁碼" onclick="pagesetup_default()">   </DIV>            </BODY>   </HTML>   更多問題歡迎來提問題網(shenzhidao.com)

摘自 網路世界無中生有

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.