JavaScript列印和預覽等

來源:互聯網
上載者:User

1.JavaScript局部列印

<input id="btnPrint" type="button" value="列印" onclick="javascript:window.print();" />

可以用樣式控制,你想讓那塊列印就列印啊,樣式如下:

<style type="text/css" media=print>
.noprint{display : none }
</style>

然後使用樣式就可以:

<p class="noprint">不需要列印的地方</p>

=============================================================

2:組件法
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:

JS 實現簡單的頁面局部列印 
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();
}

}

使用很簡單 將頁面內要列印的內容加入中間<!--startprint1-->XXXXX<!--endprint1-->
再加個列印按紐 onclick=preview(1)

source:http://blog.chinaunix.net/u/26845/showart_1087849.html

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.