JS中打印DIV

来源:互联网 发布:淘宝账号怎么找回 编辑:程序博客网 时间:2024/05/22 06:37
function printdiv(){var condition = document.getElementById("condition").value;var printpage='';if(condition==''){printpage='printear';}else if(condition=='pos'){printpage='printearByPos';}else if(condition=='dept'){printpage='printearByDept';}
<span style="white-space:pre"></span>//这种打印过后页面的有些动态加载的内容会被清空/* var headstr = "<html><head><title></title></head><body style=\"text-align: center;margin:0 auto;border-color: aqua;\">";var footstr = "</body>";var newstr = document.all.item(printpage).innerHTML;var oldstr = document.body.innerHTML;document.body.style.margin="0 auto;";document.body.innerHTML = headstr+newstr+footstr;window.print(); document.body.innerHTML = oldstr;return false; */
<span style="white-space:pre"></span>//这种是在新的窗口打开,对之前的页面没有影响    var newWindow=window.open("打印窗口","_blank");//打印窗口要换成页面的url    var docStr = printpage.innerHTML;    newWindow.document.write(docStr);    newWindow.document.close();    newWindow.print();    newWindow.close();}

0 0