打印包含在DIV中的内容
整理自http://topic.csdn.net/t/20041028/14/3499756.html
打印包含在DIV中的内容,并新开一个网页,进行打印预览。
将一下代码保存成一个网页,再新建一个名称为a.htm的文件即可运行
打开一个新的页面,读取要打印的内容,也相当于一个预览吧,在预览的同时打印页面。
代码如下:
<html><head></head><body><div id="indiv"><table><tr><td>aaaaaaaaaaaaaa</td><td>dddddddddddddd</td></tr><tr><td>cccccccccccccc</td><td>dddddddddddddd</td></tr></table></div><input type="button" onclick="print_table()" value="打印"/></body></html><script type="text/javascript">function print_table() {if(!confirm("确定要打印列表吗?")) return;var stxt=document.all.indiv.innerHTML;stxt = '<title>打印输出</title><link href="style.css" rel="stylesheet" type="text/css">' + stxt;var pwin=window.open("a.htm","print");pwin.document.write(stxt);pwin.document.close();//这句很重要哦pwin.print();}function print_alert(){window.alert("dddddddddddddddddddddddddddd");}</script>