JavaScript打印和预览等

Javascript 1357 0 2013-07-25

JavaScript打印和预览等

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)

上一篇:jquery禁止按钮的disabled属性的实现代码

下一篇:juqery实现浮动框架ifram高度自适应浏览器

讨论数量:1

天涯网魂 3 杠 5 星2013-07-25 11:36:57

js 打开,关闭,另存为,打印,直接打印,页面设置,属性,刷新
 
<OBJECT classid=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2 height=0 id=WebBrowser width=0></OBJECT>
<input name=Button onClick=document.all.WebBrowser.ExecWB(1,1) type=button value=打开>
<input name=Button onClick=document.all.WebBrowser.ExecWB(2,1) type=button value=关闭所有>
<input name=Button onClick=document.all.WebBrowser.ExecWB(4,1) type=button value=另存为>
<input name=Button onClick=document.all.WebBrowser.ExecWB(6,1) type=button value=打印>
<input name=Button onClick=document.all.WebBrowser.ExecWB(6,6) type=button value=直接打印>
<input name=Button onClick=document.all.WebBrowser.ExecWB(7,1) type=button value=打印预览>
<input name=Button onClick=document.all.WebBrowser.ExecWB(8,1) type=button value=页面设置>
<input name=Button onClick=document.all.WebBrowser.ExecWB(10,1) type=button value=属性>
<input name=Button onClick=document.all.WebBrowser.ExecWB(17,1) type=button value=全选>
<input name=Button onClick=document.all.WebBrowser.ExecWB(22,1) type=button value=刷新>
<input name=Button onClick=document.all.WebBrowser.ExecWB(45,1) type=button value=关闭>

请先登录再发表讨论。 2024-04-28

天涯网魂
3 杠 5 星
TA 的文章
TA 的随言
TA 的资源链