2014-12-02 53 views
2

创建HTML页面内联和页面打开新的标签,并显示打印视图创建HTML页面和打印到新标签的JavaScript

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

 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(); 
+0

正常工作:http://jsfiddle.net/c03fqywa /你能解释你有什么问题吗?请注意,您可能需要停用浏览器的弹出式窗口拦截器。 – 2014-12-02 11:31:29

回答

3

试试这个..

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(); 

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

+0

如何打开新标签不是新窗口 – 2014-12-02 12:29:05

2

当你需要打开一个新的选项卡,然后将其打印.. 试试这个..

<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/

在小提琴