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

html-css011

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>

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

<!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>