利用javascript實(shí)現(xiàn)web頁面中指定區(qū)域打印
更新時(shí)間:2013年10月30日 08:41:51 作者:
將需要打印的課程表的table放入div標(biāo)簽中,然后指定出需要打印的區(qū)域,最后調(diào)用window.print打印指定內(nèi)容
最近做到了web頁面課程表打印時(shí),上網(wǎng)找了一些資料,最后使用了下面的方法實(shí)現(xiàn)了我需要的功能。將需要打印的課程表的table放入div標(biāo)簽中,然后指定出需要打印的區(qū)域,最后調(diào)用window.print打印指定內(nèi)容。
示例代碼(代碼中有些內(nèi)容已省略)
復(fù)制代碼 代碼如下:
function preview() {
bdhtml = window.document.body.innerHTML;
sprnstr = "<!--startprint-->";
eprnstr = "<!--endprint-->";
prnhtml = bdhtml.substr(bdhtml.indexOf(sprnstr) + 17);
prnhtml = prnhtml.substring(0, prnhtml.indexOf(eprnstr));
window.document.body.innerHTML = prnhtml;
window.print();
}
上面的是javascript代碼,下面是html頁面代碼
復(fù)制代碼 代碼如下:
<div class="print">
<input type= "button" value= "打印課表" onclick= "preview()"/>
</div>
<div class="result">
課程表查詢結(jié)果
</div>
<center>本部分以下被打印</center>
<!--startprint-->
<div class="timetable">
<table id="table1" class ="tableresult"style="margin-left :auto;margin-right:auto;">
<tr >
<th> </th>
<th>星期一</th>
<th>星期二</th>
<th>星期三</th>
<th>星期四</th>
<th>星期五</th>
<th>星期六</th>
<th>星期日</th>
</tr>
<tr >
<th> 1,2 節(jié)</th>
<td>大學(xué)信息技術(shù)課<br>數(shù)信學(xué)院<br>501機(jī)房<br>生物專業(yè)</td>
<td></td>
<td>大學(xué)信息技術(shù)課<br>數(shù)信學(xué)院<br>501機(jī)房<br>生物專業(yè)</td>
<td></td>
<td>大學(xué)信息技術(shù)課<br>數(shù)信學(xué)院<br>501機(jī)房<br>生物專業(yè)</td>
<td></td>
<td></td>
</tr>
<tr >
<th> 3,4 節(jié)</th>
<td></td>
<td>大學(xué)信息技術(shù)課<br>數(shù)信學(xué)院<br>501機(jī)房<br>生物專業(yè)</td>
<td></td>
<td>大學(xué)信息技術(shù)課<br>數(shù)信學(xué)院<br>501機(jī)房<br>生物專業(yè)</td>
<td>大學(xué)信息技術(shù)課<br>數(shù)信學(xué)院<br>501機(jī)房<br>生物專業(yè)</td>
<td></td>
<td></td>
</tr>
<tr >
<th> 5,6 節(jié)</th>
<td>大學(xué)信息技術(shù)課<br>數(shù)信學(xué)院<br>501機(jī)房<br>生物專業(yè)</td>
<td>大學(xué)信息技術(shù)課<br>數(shù)信學(xué)院<br>501機(jī)房<br>生物專業(yè)</td>
<td>大學(xué)信息技術(shù)課<br>數(shù)信學(xué)院<br>501機(jī)房<br>生物專業(yè)</td>
<td>大學(xué)信息技術(shù)課<br>數(shù)信學(xué)院<br>501機(jī)房<br>生物專業(yè)</td>
<td></td>
<td>大學(xué)信息技術(shù)課<br>數(shù)信學(xué)院<br>501機(jī)房<br>生物專業(yè)</td>
<td>信息技術(shù)<br>數(shù)信學(xué)院<br>501機(jī)房<br>生物專業(yè)</td>
</tr>
<tr >
<th> 7,8 節(jié)</th>
<td></td>
<td>大學(xué)信息技術(shù)課<br>數(shù)信學(xué)院<br>501機(jī)房<br>生物專業(yè)</td>
<td>大學(xué)信息技術(shù)課<br>數(shù)信學(xué)院<br>501機(jī)房<br>生物專業(yè)</td>
<td>大學(xué)信息技術(shù)課<br>數(shù)信學(xué)院<br>501機(jī)房<br>生物專業(yè)</td>
<td>大學(xué)信息技術(shù)課<br>數(shù)信學(xué)院<br>501機(jī)房<br>生物專業(yè)</td>
<td></td>
<td></td>
</tr>
</table>
</div>
<!--endprint-->
<center>本部分以上被打印</center>
您可能感興趣的文章:
- JS實(shí)現(xiàn)頁面打印功能
- Javascript打印局部頁面實(shí)例
- js實(shí)現(xiàn)局部頁面打印預(yù)覽原理及示例代碼
- js或jquery實(shí)現(xiàn)頁面打印可局部打印
- js調(diào)用iframe實(shí)現(xiàn)打印頁面內(nèi)容的方法
- javascript打印大全(打印頁面設(shè)置/打印預(yù)覽代碼)
- js實(shí)現(xiàn)頁面打印功能實(shí)例代碼(附去頁眉頁腳功能代碼)
- javascript 局部頁面打印實(shí)現(xiàn)代碼
- 用javascript實(shí)現(xiàn)頁面打印的三種方法
- JS實(shí)現(xiàn)頁面打印(整體、局部)
相關(guān)文章
創(chuàng)建基于Bootstrap的下拉菜單的DropDownList的JQuery插件
這篇文章主要介紹了創(chuàng)建基于Bootstrap的下拉菜單的DropDownList的JQuery插件 的相關(guān)資料,需要的朋友可以參考下2016-06-06關(guān)于JavaScript遞歸經(jīng)典案例題詳析
遞歸是算法中一個(gè)重要的解法,因此,有必要單拎出來講講,所以下面這篇文章主要給大家介紹了關(guān)于JavaScript遞歸經(jīng)典案例題的相關(guān)資料,需要的朋友可以參考下2021-07-07Javascript客戶端將指定區(qū)域?qū)С龅絎ord、Excel的代碼
Javascript 瀏覽器客戶端導(dǎo)出指定區(qū)域到Word、Excel,非常不錯(cuò)的應(yīng)用實(shí)例2008-10-10