标签:
<html> <head> <script language="javascript"> function printdiv(printpage) { var headstr = "<html><head><title></title></head><body>"; var footstr = "</body>"; //var newstr = document.all.item(printpage).innerHTML; var newstr = document.getElementById(printpage).innerHTML; var oldstr = document.body.innerHTML; document.body.innerHTML = headstr+newstr+footstr; window.print(); document.body.innerHTML = oldstr; return false; } </script> <title>div print</title> </head> <body> //HTML Page //Other content you wouldn‘t like to print <input name="b_print" type="button" class="ipt" onClick="printdiv(‘div_print‘);" value=" Print "> <div id="div_print"> <h1 style="Color:Red">The Div content which you want to print</h1> <div style="page-break-before:always;"><br /></div> <h1 style="Color:Red">The Div content which you want to print</h1> <div style="page-break-before:always;"><br /></div> </div> //Other content you wouldn‘t like to print //Other content you wouldn‘t like to print </body> </html>
原理:大家window.print()函数可以打印网页,但是通常我们并不想打印整个页面的所有内容。这个时候就可以利用js取到原来整个页面的内容,和需要打印的区域(div)的内容,然后运行函数,将页面替换成只有div中的内容,执行print()函数,然后再将页面还原。
控制分页只需要这一条语句:<div style="page-break-before:always;"><br /></div>
标签:
原文地址:http://www.cnblogs.com/doubilaile/p/4866838.html