最近在做项目时候,遇到一个打印页面局部的功能,网上看了些,大体都一样,以下是代码:

<script type="text/javascript">
 仅仅打印标签部分
 function printTag2() {
 bdhtml = window.document.body.innerHTML;
 sprnstr = "<!--startprint-->";//开始标记
 eprnstr = "<!--endprint-->";//结束标记
 prnhtml = bdhtml.substr(bdhtml.indexOf(sprnstr) + 17);
 prnhtml = prnhtml.substring(0, prnhtml.indexOf(eprnstr));
 window.document.body.innerHTML = prnhtml;
 window.print();
    }
 </script> 

 

正文部分:

            <!--startprint-->
               <s:property value="vehicleFile.motorVehicleFiles.ownerName"/><br/>
               <s:property value="vehicleFile.motorVehicleFiles.vehicleType.vehicleTypeName"/>&nbsp;&nbsp;<s:property value="vehicleFile.motorVehicleFiles.licensePlateNumber"/>&nbsp;&nbsp;<s:property value="vehicleFile.motorVehicleFiles.dabh"/><br/>
               <s:property value="_MotorCehicleType.motorCehicleTypeName"/><br/>
               <s:property value="vehicleFile.ccbh.archivesAddress"/><br/>
           <img src="${pageContext.request.contextPath}/<s:property value="url" />"
             width="120" height="30" οnclick="printTag2();" /><br/>
              V&nbsp;<s:property value="vehicleFile.vehicleFileID"/><br/>
            <!--endprint-->

以上功能是可以实现局部打印的,但是有个疑问,就是这段代码:window.document.body.innerHTML = prnhtml;

功能是让页面只显示<!--startprint-->和<!--endprint-->标签包含的内容,但是有的时候不想把其他的内容不显示怎么办呢?

我试过一个办法  就是把这句注释了:

window.document.body.innerHTML = prnhtml;

但是注释了 这句,打印出来的就是整个页面了。

求大神赐教!