一、去掉页眉页脚的打印。虽然后面的JS函数中,也有类似的功能,经实践检验似乎不起作用。下面的代码是有效的,并且可以调整上、下边距。
<style type="text/css" id="style1">
@page { margin-top:80px; margin-bottom:30px;}
</style>
二、显示在页面上的“打印”按钮。
<input type="button" name="button_print" value="打印" onclick="javascript:printHTML()">
三、JS代码。
<script type="text/javascript">
/**打印页面*/
function printHTML(_this) {
// 获取当前页的html代码
var bdhtml = window.document.body.innerHTML;
// 通过id获取需要打印的页面
var printHtml = document.getElementById('form-div').innerHTML;
// 需要打印的页面
window.document.body.innerHTML = printHtml;
if (!!window.ActiveXObject || "ActiveXObject" in window) { //是否ie
remove_ie_header_and_footer();
}
//调用打印
window.print();
// 还原界面
window.document.body.innerHTML = bdhtml;
window.location.reload();
}
//去掉页眉、页脚
function remove_ie_header_and_footer() {
var hkey_path;
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>