js局部打印

因为需要用到这个东西,所以很无耻的拾人牙慧,收在博客里了~

?
<!DOCTYPE html>
< html >
< head >
< meta http-equiv = "Content-Type" content = "text/html; charset=utf-8" />
< title >无标题文档</ title >
< script type = "text/javascript" >  
    function doPrint() {   
        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 >
</ head >
 
< body >
< p >1</ p >
< p >2</ p >
<!--startprint--> <!--注意要加上html里star和end的这两个标记,之前没加,一直没效果,谁叫咱们菜呢~-->
< h1 >打印标题</ h1 >
< p >打印内容~~</ p >
<!--endprint-->
< button type = "button" onclick = "doPrint()" >打印</ button >
< p >1</ p >
< p >2</ p >
</ body >
</ html >

 原文地址:http://www.cnblogs.com/mofish/archive/2012/02/27/2370186.html

你可能感兴趣的:(js)