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>

 

摘自 沫魚的前端世界

發佈留言