在javascript中打印div内容 [英] print div contents in javascript

查看:104
本文介绍了在javascript中打印div内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的印刷html在下面

 < div id =DivIdToPrintstyle =border:1px solid#000000; > 
fdghgjhghjkjk
< div id =in> TO is not be that that is the question< / div>
< / div>



和javascript代码是:

  function printDiv()
{
var divToPrint = document.getElementById('DivIdToPrint');
var newWin = window.open('','Print-Window','width = 400,height = 400,top = 100,left = 100');
newWin.document.open();
newWin.document.write('< html>< body onload =window.print()>'+ divToPrint.innerHTML +'< / body>< / html>');
newWin.document.close();
setTimeout(function(){newWin.close();},10);

}

我想打印DivIdToPrintdiv,但不要在div内容中显示。

解决方案在您的JS代码中添加样式以隐藏indiv < style> #in {display:none}< / style> ,如下所示:

  function printDiv()
{
var divToPrint = document.getElementById('DivIdToPrint');
var newWin = window.open('','Print-Window','width = 400,height = 400,top = 100,left = 100');
newWin.document.open();
newWin.document.write('< html>< head>< style> #in {display:none}< / style>< body onload =window.print()>' + divToPrint.innerHTML + '< /体>< / HTML>');
newWin.document.close();
setTimeout(function(){newWin.close();},10);

}


My print html is below

<div id="DivIdToPrint" style="border:1px solid #000000;">
 fdghgjhghjkjk
<div id="in">TO be are not to be that is the question</div>
</div>

and javascript code is :

function printDiv()
{
var divToPrint=document.getElementById('DivIdToPrint');
var newWin=window.open('','Print-Window','width=400,height=400,top=100,left=100');
newWin.document.open();
newWin.document.write('<html><body   onload="window.print()">'+divToPrint.innerHTML+'</body></html>');
newWin.document.close();
setTimeout(function(){newWin.close();},10);

}

I want to print "DivIdToPrint" div but don't show "in " div content.

解决方案

In your JS code add style to hide the "in" div <style>#in {display:none}</style>, like this:

function printDiv()
{
var divToPrint=document.getElementById('DivIdToPrint');
var newWin=window.open('','Print-Window','width=400,height=400,top=100,left=100');
newWin.document.open();
    newWin.document.write('<html><head><style>#in {display:none}</style><body   onload="window.print()">'+divToPrint.innerHTML+'</body></html>');
newWin.document.close();
setTimeout(function(){newWin.close();},10);

}

这篇关于在javascript中打印div内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆