js打印網頁的特定內容三步搞定


1.頁面添加打印按鈕

<div>
<a onclick="exportPuts();" class="btn btn-add" style="margin-bottom:2px;margin-right: 10px"> <i class="icon-add"></i> 打印</a>
</div>

2.確定需要打印的頁面位置,用標簽 <!--startprint-->和標簽 <!--endprint--> 將需要打印的位置划分出來

<!--startprint-->
		<div class="row-fluid">
			<div class="span12">
				<div class="widget blue">
					<div class="widget-title"><h4><i class="icon-align-        
                                                        left"></i> 同比分析</h4></div>
					<div class="widget-body">
						<div id="main_1"></div>
						<div id="main_2">
							<br><br><br><br>
							<div id="text1"></div>
							<br><br>
							<div id="text2"></div>
						</div>
					</div>
				</div>
			</div>
		</div>
<!--endprint-->        

  3.js添加打印函數

<script type="text/javascript">
function exportPuts(){	
		 bdhtml=window.document.body.innerHTML;
         sprnstr="<!--startprint-->";//必須在頁面添加<!--startprint-->和<!--endprint-->而且需要打印的內容必須在它們之間
         eprnstr="<!--endprint-->";
         prnhtml=bdhtml.substr(bdhtml.indexOf(sprnstr)+18);
         prnhtml=prnhtml.substring(0,prnhtml.indexOf(eprnstr));
 		 var newWin= window.open("");//新打開一個空窗口
     	 newWin.document.body.innerHTML=prnhtml;
  		 newWin.document.close();//在IE瀏覽器中使用必須添加這一句
  		 newWin.focus();//在IE瀏覽器中使用必須添加這一句
 		 newWin.print();//打印
  		 newWin.close();//關閉窗口 
                                   }
</script>

  

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM