HTML 怎么写一个打印预览页面。

html-css09

HTML 怎么写一个打印预览页面。,第1张

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>Insert title here</title>

<script type="text/javascript">

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()

}

}

function xx(){

window.print()

}

</script>

</head>

<body>

<samp>预览功能可以使用谷歌浏览器,ie预览功能同上,其他好像不支持</samp>

<samp style="color: red">直接粘贴到页面(html)中就可以使用</samp>

<!--startprint1-->

<!--打印内容开始-->

<div id=sty>

  <img alt="" src="http://img4.duitang.com/uploads/item/201401/25/20140125231152_VcdwM.jpeg" 

  style="width: 500pxheight: 500px">

</div>

<!--打印内容结束-->

<!--endprint1-->

<input type=button name='button_export' title='打印1' onclick=preview(1) value=打印图片>

<input type="button" value="打印整个网页" onclick="xx()">

</body>

</html>

<HTML><HEAD><TITLE>打印-页面设置-打印预览-关闭窗口</TITLE>

<SCRIPT language=javascript>

function printsetup()

{

// 打印页面设置

wb.execwb(8,1)

}

function printpreview()

{

var ht1 = document.getElementByIdx_x("h")

ht1.style.display="none"//隐藏不必打印的部分,该隐藏只在预览中有效,真正打印时要用css控制

wb.execwb(7,1)// 打印页面预览

ht1.style.display="" //预览完再将隐藏的部分显示出来

}

function printit()

{

 if (confirm('确定打印吗?'))

{

wb.execwb(6,6)

}

}

</script>

<style type="text/css" media=print>

.noprint{display : none } //不打印

</style>

</HEAD>

<BODY>

<!-- div h 中的内容不打印 -->

<DIV id="h" align=center class="noprint">

<OBJECT id=wb height=0 width=0 classid=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2></OBJECT>

<INPUT onclick=javascript:printit() type=button value=打印 />

<INPUT onclick=javascript:printsetup()type=button value=打印页面设置 />

<INPUT onclick=javascript:printpreview()type=button value=打印预览 />

<INPUT onclick=javascript:window.close()type=button value=关闭 />

</DIV>

要打印的正文

</BODY>

</HTML>

核心部分就这些(起作用的?):

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/htmlcharset=utf-8" />

<script language="javascript">

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()

}

</script>

</head>

<body>

<form id="WebForm1" method="post" ><div>1234567890</div>

<center>本部分以上不被打印</center>

<!--startprint-->

<div align="center">

<table border="1">

<tr bordercolor="#FFFFFF">

<th bordercolor="#FFFFFF">销售单</th>

<th>工资单</th>

</tr>

<tr>

<td>销售单</td>

<td>销售123</td>

</tr>

</table>

</div>

<!--endprint-->

<center>本部分以下不被打印</center>

<div align="center">

<input type="button" name="print" value="预览并打印" onClick="preview()">

</div>

</form>

</body>

</html>