去掉浏览器打印默认的页头页尾
<!DOCTYPE html>
<html>
<head>
<title>print</title>
<meta charset="utf-8">
<style media="print">
@page {
size: auto;
margin: 0mm;
}
</style>
</head>
<body>
<p style="width:300px; height:300px;border: 1px solid blue;">
打印内容
</p>
<button onclick='print_page()'>print</button>
</body>
<script type="text/javascript">
function print_page() {
if (!!window.ActiveXObject || "ActiveXObject" in window) {
remove_ie_header_and_footer();
}
window.print();
};
function remove_ie_header_and_footer() {
var hkey_root, hkey_path, hkey_key;
hkey_path = "HKEY_CURRENT_USER\\Software\\Microsoft\\Internet Explorer\\PageSetup\\";
try {
var RegWsh = new ActiveXObject("WScript.Shell");
RegWsh.RegWrite(hkey_path + "header", "");
RegWsh.RegWrite(hkey_path + "footer", "");
} catch (e) {}
}
</script>
</html>
设置浏览器打印,不需要手动勾选背景图形,即可打印出背景
<style type="text/css">
@media print {
body {-webkit-print-color-adjust: exact;}
}
</style>