JS打印网页上指定的id区域内容

demo示例:

<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		<title>JS打印网页上指定的id区域内容</title>
		<script type="text/javascript" language="javascript">
			function  printPage(){   
			  var newWin=window.open('about:blank', '', '');   
			  var titleHTML=document.getElementById("doctitle").innerHTML;   
			  newWin.document.write(titleHTML);   
			  newWin.document.location.reload();   
			  newWin.print();   
			}
		</script>
	</head>
	<body>表格外的区域是不打印的哦
		<br>
		<br>
		<div>
			<img src="https://static.cnblogs.com/images/adminlogo.gif" />
		</div>
		<div id="doctitle" class="doctitle">
			<table width="100%" border="1" cellspacing="1" cellpadding="0">
				<tr>
					<td width="105">站名</td>
					<td width="181">网址</td>
					<td width="112">说明</td>
				</tr>
				<tr>
					<td>DIVCSS5</td>
					<td>www.divcss5.com</td>
					<td>CSS学习</td>
				</tr>
				<tr>
					<td>CSS5</td>
					<td>www.css5.com.cn</td>
					<td>CSS切图</td>
				</tr>
			</table>
		</div>
		<br>
		<br>
		<button name="test" type="button" onclick="printPage()" value="val">只打印表格中的内容</button>
	</body>

</html>

  

posted @ 2019-05-15 21:01  扬阳  阅读(684)  评论(0编辑  收藏  举报