tpg*_*lan 7 html javascript printing jquery html5
假设我在JavaScript中有这样的变量:
var h = "<html><head></head><body><h1>Example</h1><p>This is a variable I want to print to the printer with the browser!</p></body></html>"
Run Code Online (Sandbox Code Playgroud)
如何将此变量的漂亮呈现内容发送到打印机?
我试过了:
var w = window.open();
Run Code Online (Sandbox Code Playgroud)
然后:
$(w).html(h);
Run Code Online (Sandbox Code Playgroud)
然后:
w.print();
Run Code Online (Sandbox Code Playgroud)
但问题是弹出窗口阻止程序阻止我的新窗口打印页面.
任何人有任何想法?
非常感谢!
现在通常我不会内联完成这一切。但就这个问题而言,我觉得这样读起来更容易。基本上设置一个允许可打印区域的媒体样式表。然后将您的 html 分配给该 div。现在,当点击打印时,只有 #printableArea 将被发送到打印机。
<html>
<head>
<style type="text/css">
#printableArea { display: none; }
@media print
{
#non-printableArea { display: none; }
#printableArea { display: block; }
}
</style>
<script type="text/javascript">
$(document).ready(function () {
var h = "<html><head></head><body><h1>Example</h1><p>This is a variable I want to print to the printer with the browser!</p></body></html>";
$("#printableArea").html(h.find("body").html());
});
</script>
</head>
<body>
<div id="printableArea"></div>
<div id="non-printableArea"></div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)