打印预览的实现

本文介绍了一种使用JavaScript和HTML实现网页打印功能的方法。通过定义特定的JavaScript函数,可以将指定的网页内容转换为打印预览格式,并允许用户进行打印操作。此方案移除了不必要的元素,如按钮和链接,以确保打印内容的整洁。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

a.jsp是要打印的页面

 

<html>
<head>
<script language="javascript">
function fmtPrint(printPlace,w,h){
var sarg=new Array();
var sdata=document.all.item(printPlace);
sarg[
0]=sdata.outerHTML;
window.showModalDialog(
"print.jsp",sarg,"dialogWidth:"+w+"px;dialogHeight:"+h+"px;center:yes;help:no;status:no;resizable:yes");
return;
}

</script>
</head>
<body>
<div id="printPlace">
希望打印的内容写在里面
...........
...........
...........
...........
...........
</div>
<img onClick="fmtPrint('printPlace', 800, 600);" name="Print" border="0" style="cursor:hand" src="images/dayin.gif">
</body>
</html>

print.jsp是打印预览页面

 

<html>
<head>
<base target="_self">
<script language="javascript">
<!--
var dada = dialogArguments;
var da1 = dada[1]; 
function loaddatas(){
var tagBody;
var pf = document.all.item('printfield');
pf.insertAdjacentHTML(
'beforeEnd',da1); 
    
var removeRadio = document.all.tags("input");
for(var i=0; i<removeRadio.length; i++){
tagBody 
= removeRadio[i];
if (tagBody.getAttribute("type"== "radio"||tagBody.getAttribute("type"== "checkbox")
{
tagBody.style.display 
= 'none';
}

}

    
var removeHref = document.all.tags("A");
for(var i=0; i<removeHref.length; i++){
tagBody 
= removeHref[i];
if (tagBody.getAttribute("href"!= null
{
tagBody.setAttribute(
"href","javascript:void(0)");
}

}

    
var removeImg = document.all.tags("img");
for(var i=0; i<removeImg.length; i++){
tagBody 
= removeImg[i];
if ( tagBody.name != 'printButton' ) 
{
tagBody.style.display 
= 'none';
}

}

return;
}

function startprint(){
document.all.item(
"startbutt").style.display='none';
window.print();
window.close();
return;
}

//-->
</script> 
</head>
<body leftMargin=0 topMargin=0 marginheight="0" marginwidth="0" onload="javascript:loaddatas();" style="word-break:break-all">
<table align="center">
<tr>
<td align="center" id="printfield"></td>
</tr>
</table>
<table width="100%" id="startbutt">
<tr>
<td align=center>
<onclick="javascript:startprint();"><img border="0" name="printButton" src="images/dayin.gif" style="vertical-align:middle; cursor:hand"></a>
</td>
</tr>
</table>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值