js 网页列印
来源:互联网 发布:seo网站建设 编辑:程序博客网 时间:2024/06/06 15:48
通過頁面上的一個按鈕的點擊事件,來實現網頁打印.
在這裡講述利用js來實現打印,方法很簡單,講兩種:
第一種:截取
第二種利用樣式使不想打印的不進行打印.
截取js:
functionPrintDoc()
{
// 獲取頁面的所有的代碼
varallContent = window.document.body.innerHTML;
vars = allContent;
// 定義兩個變量
varPrintSt = '<!--PrintSt-->';
varPrintEnd = '<!--PrintEnd-->';
// 截取需要里列印的數據段,這裡有時候還需要自己調整一下
s = allContent.substr(0,allContent.indexOf(PrintEnd) - (allContent.indexOf(PrintSt) + PrintSt.length +1));
// 將要列印的數據顯示在頁面上
window.document.body.innerHTML = s;
// 打印
window.print();
// 回復原來的頁面
window.document.body.innerHTML =allContent;
returnfalse;
}
<div>
<table>
<tr>
<td>http://hi.baidu.com/new/zhong635976601</td>
<td>http://hi.baidu.com/new/zhong635976601</td>
</tr>
<tr>
<td>http://hi.baidu.com/new/zhong635976601</td>
<td>http://hi.baidu.com/new/zhong635976601</td>
</tr>
</table>
</div>
<!--PrintSt-->
<div>
<table>
<tr>
<td> qq:635976601</td>
<td> qq:635976601</td>
</tr>
<tr>
<td> qq:635976601</td>
<td> qq:635976601</td>
</tr>
</table>
</div>
<!--PrintEnd-->
<div>
<table>
<tr><td>http://hi.baidu.com/new/zhong635976601</td></tr>
</table>
</div>
調用上面的js后會列印成
若是js中直接寫”window.print();”
則效果是:
經過上面的舉例講解,你應該明白了,怎麼利用截取字符串的方式進行網頁打印的吧.不明白的可以留言
2. 利用樣式使不想打印的不進行打印.
首先添加樣式:
<style>
@mediaprint
{
.noprint
{
display:none;
}
}
</style>
現在還是以上面的例子解釋:
<div>
<table>
<tr>
<td> qq:635976601</td>
<td> qq:635976601</td>
</tr>
<tr>
<td> qq:635976601</td>
<td> qq:635976601</td>
</tr>
</table>
</div>
若還是只想打印這一段,而不像打印其他的,此時很簡單的一個方法就是
<div class=”noprint”>
<table>
<tr>
<td>http://hi.baidu.com/new/zhong635976601</td>
<td>http://hi.baidu.com/new/zhong635976601</td>
</tr>
<tr>
<td>http://hi.baidu.com/new/zhong635976601</td>
<td>http://hi.baidu.com/new/zhong635976601</td>
</tr>
</table>
</div>
<!--PrintSt-->
<div>
<table>
<tr>
<td>qq:635976601</td>
<td> qq:635976601</td>
</tr>
<tr>
<td> qq:635976601</td>
<td> qq:635976601</td>
</tr>
</table>
</div>
<!--PrintEnd-->
<div class=”noprint>
<table>
<tr><td>http://hi.baidu.com/new/zhong635976601</td></tr>
</table>
</div>
這樣就可以輕鬆實現了,是不是很簡單啊.你是不是已經學會了呢?
不明白的可以留言問我
- js 网页列印
- web列印的js簡單寫法10/26
- web列印
- 列印版面設定
- 從瀏覽器列印報表
- 付款凭证列印
- ZVT003出闸纸列印
- PHP列印条码
- CMYK列印颜色
- 自定义纸张列印
- 頁面列印
- web 打印 Excel列印方式
- 以Mime方式列印Excel
- 【JS】:JS实现网页计算器
- 网页加密JS代码
- js网页编辑器
- 网页刷新推荐-JS
- 网页,js中文问题
- CheckBoxList 獲取選中的值或判斷被選中項
- [DLL]使用def文件从dll导出
- oracle number sequence 和 java.util.Long范围关系oracle sequence的取值范围是
- OpenErp学习笔记
- uploadify 上传大文件 出现的 HTTP error 问题的方法
- js 网页列印
- Xcode调试打印方法
- google-gson 使用及GsonBuilder设置
- Maven学习之maven核心,pom.xml详解
- WM_USER和WM_APP
- ASCII,Unicode,UTF-8,GB2312简介
- ibatis事务
- win32编程:invalidateRect;updateWindow;BeginPaint;EndPaint
- php的PDO类中bindValue和bindParam的区别