1. 程式人生 > >js實現區域列印

js實現區域列印

    查了一下網上的資料,找到一個比較合適的例子,做一個記錄

<!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/html; charset=utf-8" />
<script language="javascript">
function preview0(){
window.print();
}
function preview(){
bdhtml = window.document.body.innerHTML;
sprnstr = "<!--start-->";
eprnstr = "<!--end-->";
prnhtml = bdhtml.substr(bdhtml.indexOf(sprnstr));
prnhtml = prnhtml.substring(0,prnhtml.indexOf(eprnstr));
window.document.body.innerHTML = prnhtml;
window.print();
}
</script>
<style media="print">
    @page {
      size: auto;  /* auto is the initial value */
      margin: 7mm; /* this affects the margin in the printer settings */
    }
</style>
</head>
<body>
<form id="WebForm1" method="post" > <div>1234567890</div>
<!--start--> 
<div align="center">
<center>本部分以上不被列印</center> 
<table border="1"> 
<tr bordercolor="#FFFFFF" ;>
<th bordercolor="#FFFFFF">銷售單</th>
<th>工資單</th>
</tr>
<tr>
<td>銷售單</td>
<td>銷售123</td>
</tr> 
</table> 
</div> 
<!--end--> 
<center>本部分以下不被列印</center> 
<div align="center"> 
<input type="button" name="print" value="預覽並列印" onClick="preview()"> 
</div>
</form>
</body>
</html>