创建html页面并在javascript中打印到新标签页

时间:2014-12-02 11:26:50

标签: javascript jquery html

创建html页面为内联,该页面打开到新选项卡并显示打印视图

我试过这段代码但没有工作..

     var mywindow = window.open('', 'Print  Report', 'height=400,width=600');
     mywindow.document.write('<html><head><title>Print  Report</title>');
     mywindow.document.write('</head><body ><table  border="1" style="width: 500px; height: 300px;">');
     mywindow.document.write(htmlTable);
     mywindow.document.write('</table></body></html>');
     mywindow.open().print();

2 个答案:

答案 0 :(得分:5)

试试这个..

var winPrint = window.open('', '', 'left=0,top=0,width=800,height=600,toolbar=0,scrollbars=0,status=0');
winPrint.document.write('<title>Print  Report</title><br /><br /> Hellow World');
winPrint.document.close();
winPrint.focus();
winPrint.print();
winPrint.close(); 

如果窗口未打开..请检查弹出窗口是否被阻止:)..

答案 1 :(得分:3)

因为你需要打开一个新的标签,然后打印.. 试试这个..

<div id="toNewWindow">
    <p>Your content here</p>
</div>
<a href="javascript:;" id="print">Open</a>
<script>
function nWin() {
  var w = window.open();
  var html = $("#toNewWindow").html();

    $(w.document.body).html(html);
    w.print();
}

$(function() {
    $("a#print").click(nWin);
});</script>

小提琴:: http://jsfiddle.net/Sarathv15/8dXvt/420/