使用 PrintArea 打印 HTML 页面的内容

这篇博客介绍了如何利用PrintArea插件实现点击按钮后,浏览器弹出新窗口打印HTML页面中的表格内容,展示了具体的效果和完整的页面代码。

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

【本例效果】


点击“打印按钮后,浏览器会弹出新窗口打印表格,如下图:



【实现步骤】
1. 下载 PrintArea 库,在页面中导入相应的 jQuery 库:
<!-- 导入 jQuery -->
<script src="js/jquery-1.12.2.min.js"></script>
<!-- 导入 PrintArea -->
<script src="js/jquery.PrintArea.js"></script>
2. 为“打印”按钮绑定点击事件:

<script type="text/javascript">
$(document).ready(function(){
    // 绑定“打印”按钮的点击事件,"test123" 为“打印”按钮的 class 属性值;
    $(".test123").click(function () {
    // 打印 class 为 print123 的节点;
        $(".print123").printArea();
    });
});
</script>

搞定!


【附页面完整代码】

<!DOCTYPE html>
<html lang="zh-CN">
	<head>
		<meta charset="utf-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<title>打印页面示例</title>
		<!-- 导入 jQuery -->
		<script src="js/jquery-1.12.2.min.js"></script>
		<!-- 导入 PrintArea -->
		<script src="js/jquery.PrintArea.js"></script>
		<script type="text/javascript">
			$(document).ready(function(){
				// 绑定打印按钮的点击事件;
				$(".test123").click(function () {
					// 打印 class 为 print123 的节点;
					$(".print123").printArea();
				});
			});
		</script>
	</head>
	<body>
		<h1>你好,世界!</h1>
		<div class="print123">
			<table>
				<tr>
					<td>1</td>
					<td>2</td>
					<td>3</td>
					<td>4</td>
				</tr>
				<tr>
					<td>5</td>
					<td>6</td>
					<td>7</td>
					<td>8</td>
				</tr>
				<tr>
					<td>9</td>
					<td>10</td>
					<td>11</td>
					<td>12</td>
				</tr>
			</table>
		</div>
		<button type="button" class="test123">
			打印
		</button>
	</body>
</html>


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值