print() 方法用于打印当前窗口的内容
调用 print() 方法所引发的行为就像用户单击浏览器的打印按钮。通常,这会产生一个对话框,让用户可以取消或定制打印请求。
语法:
window.print();
demo
<!DOCTYPE html>
<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="myPrint(document.getElementById('print'))">打 印</button>
<script>
function myPrint(obj){
var newWindow=window.open("打印窗口","_blank");
var docStr = obj.innerHTML;
newWindow.document.write(docStr);
newWindow.document.close();
newWindow.print();
newWindow.close();
}
</script>
</body>
</html>
本文介绍了一种使用JavaScript实现的网页打印功能,通过调用window.print()方法来模拟浏览器打印行为,同时提供了示例代码展示如何仅打印网页中特定区域的内容。
606

被折叠的 条评论
为什么被折叠?



