Js打印指定区域的内容怎么写

JavaScript011

Js打印指定区域的内容怎么写,第1张

function preview(oper) {

2 if (oper <10){

3 bdhtml=window.document.body.innerHTML//获取当前页的html代码

4 sprnstr="<!--startprint"+oper+"-->"//设置打印开始区域

5 eprnstr="<!--endprint"+oper+"-->"//设置打印结束区域

6 prnhtml=bdhtml.substring(bdhtml.indexOf(sprnstr)+18)//从开始代码向后取html

7

8 prnhtml=prnhtml.substring(0,prnhtml.indexOf(eprnstr))//从结束代码向前取html

9 window.document.body.innerHTML=prnhtml

10 window.print()

11 window.document.body.innerHTML=bdhtml

12

13 }

14 else{

15 window.print()

16 }

打印网页中定义的部分内容的实现方法正常情况下的打印是使用 window.print()直接整页打印,但如果需要打印网页中定义的部分内容,则可使用如下的方法:

这个插件是我在项目中多次使用的插件,插件主要功能是打印当前页面(当前页面,非当前屏幕)

链接: https://pan.baidu.com/s/1nQQX8ov1YJWkSXUnQy-_sA 密码:akpq

使用方式

1.引用文件中的两个js

这里jquery版本只有1.4.4,所以建议将打印的页面和后台其他页面分开,避免发生冲突

2.定义function

3.html

注意点:需使用低版本jq,高版本不兼容.

可以通过样式表(CSS)分别对屏幕和打印机指定不同的样式,比如

<style media="screen">

#div1 {width:1000px}

#div2 {display:block}

</style>

<style media="print">

#div1 {width:600px}

#div2 {display:none}

</style>

<div id=div1>这个DIV在屏幕中的显示宽度是1000像素,在打印机中是600像素</div>

<div id=div2>这个DIV在屏幕中会显示,在打印机中则自动消失</div>