JavaScript学习

js 打印指定区域

 

方法一:

<script language="javascript" type="text/javascript">

&#160;&#160;&#160; function printpage(myDiv){    
&#160;&#160;&#160; //var newstr = document.all.item(myDiv).innerHTML;    
&#160;&#160;&#160; var newstr = document.getElementById(myDiv).innerHTML;    
&#160;&#160;&#160;&#160; alert(newstr);    
&#160;&#160;&#160; var oldstr = document.body.innerHTML;    
&#160;&#160;&#160; document.body.innerHTML = newstr;    
&#160;&#160;&#160; window.print();    
&#160;&#160;&#160; document.body.innerHTML = oldstr;    
&#160;&#160;&#160; return false;    
&#160;&#160;&#160; }&#160;
&#160;&#160;&#160; </script>

&#160;&#160; <div id="myDiv"&#160; >说明:打印整个div区域的内容,如果包含按钮,则按钮也会打印出来!  
&#160;&#160;&#160; <div>    
&#160;&#160;&#160;&#160;&#160; 打印内容打印内容打印内容打印内容打印内容打印内容打印内容打印内容    
&#160;&#160;&#160; adfhioasdhfiohasdofihosdhfosdhfiosdhfiosdfhsdfhsdifsidfiosdfhosdhf    
&#160;&#160;&#160;&#160;&#160;&#160;&#160; </div>    
&#160;&#160;&#160; <input type="button" id="bt" onclick="javascript:printpage('myDiv')"&#160;&#160; value="打印" />    
&#160;&#160;&#160; </div>

方法二:

<script language="javascript" type="text/javascript">  
&#160;&#160;&#160;&#160;&#160;&#160;&#160; function doPrint() {    
&#160;&#160;&#160;&#160;&#160;&#160;&#160; bdhtml=window.document.body.innerHTML;    
&#160;&#160;&#160;&#160;&#160;&#160;&#160; sprnstr="<!--startprint-->";    
&#160;&#160;&#160;&#160;&#160;&#160;&#160; eprnstr="<!--endprint-->";    
&#160;&#160;&#160;&#160;&#160;&#160;&#160; prnhtml=bdhtml.substr(bdhtml.indexOf(sprnstr)+17);    
&#160;&#160;&#160;&#160;&#160;&#160;&#160; prnhtml=prnhtml.substring(0,prnhtml.indexOf(eprnstr));    
&#160;&#160;&#160;&#160;&#160;&#160;&#160; window.document.body.innerHTML=prnhtml;    
&#160;&#160;&#160;&#160;&#160;&#160;&#160; window.print();    
&#160;&#160;&#160;&#160;&#160;&#160;&#160; }    
&#160;&#160;&#160;&#160;&#160;&#160;&#160; </script>

<div>  
&#160;&#160;&#160; <!--startprint-->说明:这是开始打印的位置    
&#160;&#160;&#160; 打印内容打印内容打印内容打印内容打印内容打印内容打印内容打印内容    
&#160;&#160;&#160; adfhioasdhfiohasdofihosdhfosdhfiosdhfiosdfhsdfhsdifsidfiosdfhosdhf    
&#160;&#160;&#160; <!--endprint-->说明:这是结束打印的位置    
&#160;&#160;&#160; </div>    
&#160;&#160;&#160; <input type="button" id="bt" onclick="javascript:doPrint()"&#160;&#160; value="打印" />    
&#160;&#160;&#160; </div>

你可能感兴趣的:(JavaScript学习)