在代码中做以下得标记:
<!--starttitle-->
您要打印的标题
<!--endtitle-->
<!--startprint-->
您要打印的内容部分;
<!--endprint-->
打印的按钮:
<input type=button name="Print" value="打印" onclick="print_print()" language="javascript" >
脚本如下:
<script language="javascript">
function print_print()
{
bdhtml=window.document.body.innerHTML;
sprnstr="<!--startprint-->";
eprnstr="<!--endprint-->";
stitle = "<!--starttitle-->";
etitle ="<!--endtitle-->";
title = bdhtml.substr(bdhtml.indexOf(stitle)+17);
title = title.substring(0,title.indexOf(etitle));
prnhtml=bdhtml.substr(bdhtml.indexOf(sprnstr)+17);
prnhtml=prnhtml.substring(0,prnhtml.indexOf(eprnstr));
if (prnhtml!="")
{
prnhtml="<h2>"+title+"</h2><br>"+prnhtml+"<p align='center'>版权所有</p>";
window.document.body.innerHTML=prnhtml;
window.print();
location.reload();
}
}
function printpr() //预览函数
{
var OLECMDID = 7;
var PROMPT = 1;
var WebBrowser = '<OBJECT ID="WebBrowser1" WIDTH=0 HEIGHT=0 CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>';
bdhtml=window.document.body.innerHTML;
sprnstr="<!--startprint-->";
eprnstr="<!--endprint-->";
stitle = "<!--starttitle-->";
etitle ="<!--endtitle-->";
title = bdhtml.substr(bdhtml.indexOf(stitle)+17);
title = title.substring(0,title.indexOf(etitle));
prnhtml=bdhtml.substr(bdhtml.indexOf(sprnstr)+17);
prnhtml=prnhtml.substring(0,prnhtml.indexOf(eprnstr));
if (prnhtml!="")
{
prnhtml="<h2>"+title+"</h2><br>"+prnhtml+"<p align='center'>版权所有</p>";
window.document.body.innerHTML=prnhtml;
document.body.insertAdjacentHTML('beforeEnd', WebBrowser);
WebBrowser1.ExecWB(OLECMDID, PROMPT);
WebBrowser1.outerHTML = "";
location.reload();
}
}
</script>
按这样的办法可以提取一个页面的n个部分合在一起。
预览功能
<input type=button name="Printpr" value="预览" onclick="printpr()" language="javascript" class=Inp>