通过查看页面代码才发现局部打印和整页打印的区别:
观察一下JS代码window.document.body.innerHTML=prnhtml; 这个代码的意思是取到待打印代码部分,然后赋值给整个页面代码,但是局部打印的代码中并没有body这个标签,所以才会有在局部打印的时候整个页面都是空的情况。
修改如下:


function
preview(oper)
{
if (oper < 10 )
{
bdhtml = window.document.body.innerHTML; // 获取当前页的html代码
sprnstr = " <!--startprint " + oper + " --> " ; // 设置打印开始区域
eprnstr = " <!--endprint " + oper + " --> " ; // 设置打印结束区域
prnhtml = bdhtml.substring(bdhtml.indexOf(sprnstr) + 18 ); // 从开始代码向后取html
prnhtml = prnhtml.substring( 0 ,prnhtml.indexOf(eprnstr)); // 从结束代码向前取html
window.document.body.innerHTML = " <body> " + prnhtml + " </body> " ;
window.print();
window.document.body.innerHTML = bdhtml;
}
else
{
window.print();
}
}
{
if (oper < 10 )
{
bdhtml = window.document.body.innerHTML; // 获取当前页的html代码
sprnstr = " <!--startprint " + oper + " --> " ; // 设置打印开始区域
eprnstr = " <!--endprint " + oper + " --> " ; // 设置打印结束区域
prnhtml = bdhtml.substring(bdhtml.indexOf(sprnstr) + 18 ); // 从开始代码向后取html
prnhtml = prnhtml.substring( 0 ,prnhtml.indexOf(eprnstr)); // 从结束代码向前取html
window.document.body.innerHTML = " <body> " + prnhtml + " </body> " ;
window.print();
window.document.body.innerHTML = bdhtml;
}
else
{
window.print();
}
}
加上body之后的局部打印就正常了!
第二中:组件法





































































