javascript 在javascript中创建html页面并打印到新标签
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27248259/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
create html page and print to new tab in javascript
提问by Prasanth A R
create html page as inline and that page open to new tab and show print view
将 html 页面创建为内联页面,该页面打开到新选项卡并显示打印视图
i tried with this code but not working..
我尝试使用此代码但不起作用..
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();
回答by Sarath
try this..
试试这个..
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();
if the window is not open .. please check whether the popup is blocked :)..
如果窗口没有打开..请检查弹出窗口是否被阻止:)..
回答by Sarath
as you need to open a new tab and then make it print .. try this..
因为你需要打开一个新标签,然后让它打印.. 试试这个..
<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>
fiddle :: http://jsfiddle.net/Sarathv15/8dXvt/420/