打印HTML页面
代码:
<!DOCTYPE>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>js print打印网页指定区域内容的简单实例</title>
</head>
<body>
<div id="print">
<hr>
打印演示区域,点击打印后会在新窗口加载这里的内容!
<hr>
</div>
<button onclick="print()">print</button>
</body>
<script>
function print(){
var obj = document.getElementById('print');
var newWindow = window.open("打印窗口","_blank");
var content = obj.innerHTML;
newWindow.document.write(content);
newWindow.document.close();
newWindow.print();
newWindow.close();
}
</script>
</html>
运行截图: