<!--startprint-->
打印区域
<!--endprint-->
<script type="text/javascript">
function preview() {
bdhtml = window.document.body.innerHTML; //获取当前页的html代码
sprnstr = "<!--startprint-->"; //设置打印开始区域
eprnstr = "<!--endprint-->"; //设置打印结束区域
prnhtml = bdhtml.substring(bdhtml.indexOf(sprnstr) + 18); //从开始代码向后取html
prnhtml = prnhtml.substring(0, prnhtml.indexOf(eprnstr)); //从结束代码向前取html
window.document.body.innerHTML = prnhtml;
window.print();
window.document.body.innerHTML = bdhtml;
}
</script>
<div id="div_print">
打印区域
</div>
<script type="text/javascript">
function printdoc() {
var printpage = document.getElementById("div_print");
var newcont = printpage.innerHTML;
var oldcont = document.body.innerHTML;
document.body.innerHTML = newcont;
window.print();
document.body.innerHTML = oldcont;
return false;
}
</script>
head中添加
<style type="text/css" media="print">
@page { margin: 0; }
</style>
<input type="button" value="清空页码" onclick="PageSetup_Null()" >
<input type="button" value="恢复页吗" onclick="PageSetup_Default()">
<script type="text/javascript">
var HKEY_Root, HKEY_Path, HKEY_Key;
HKEY_Root = "HKEY_CURRENT_USER";
HKEY_Path = "\\Software\\Microsoft\\Internet Explorer\\PageSetup\\";
//设置网页打印的页眉页脚为空
function PageSetup_Null() {
try {
var Wsh = new ActiveXObject("WScript.Shell");
HKEY_Key = "header";
Wsh.RegWrite(HKEY_Root + HKEY_Path + HKEY_Key, "");
HKEY_Key = "footer";
Wsh.RegWrite(HKEY_Root + HKEY_Path + HKEY_Key, "");
} catch (e) {
}
}
//设置网页打印的页眉页脚为默认值
function PageSetup_Default() {
try {
var Wsh = new ActiveXObject("WScript.Shell");
HKEY_Key = "header";
Wsh.RegWrite(HKEY_Root + HKEY_Path + HKEY_Key, "&w&b页码,&p/&P");
HKEY_Key = "footer";
Wsh.RegWrite(HKEY_Root + HKEY_Path + HKEY_Key, "&u&b&d");
} catch (e) {}
}
</script>