javascript 向打印机发送数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16724550/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 05:45:31  来源:igfitidea点击:

Sending data to printer

javascriptjqueryhtml

提问by Marin Horvat

Using php I created dynamical table, which contains all students from my database. I would like now to add an icon of printer, and when user clicks on that icon, to send this table to printer? I saw it on many pages, but how it's done? Tnx in advance...

我使用 php 创建了动态表,其中包含我数据库中的所有学生。我现在想添加一个打印机图标,当用户单击该图标时,将此表发送到打印机?我在很多页面上都看到过,但它是如何完成的?提前通知...

回答by Siamak A.Motlagh

Simply use window.print().
Example in jQuery:

只需使用window.print().
jQuery 中的示例:

<script>
$(function() {
    $("#print").click(function() {
        window.print();
    });
});
</script>
<a id='print'>Print ME</a>

Example in JavaScript:

JavaScript 中的示例:

<script>
function printMe() {
    window.print()
}
</script>
<input type="button" value="Print" onclick="printMe()">

回答by AlienWebguy

Aside from window.print()it should be noted that JavaScript can't send the data to the printer directly - it can prompt the print dialog for you though. IE6 used to allow that, and visiting certain sites would make for surprises in your printer tray later that evening.

除了window.print()应该注意的是,JavaScript 不能直接将数据发送到打印机 - 不过它可以为您提示打印对话框。IE6 曾经允许这样做,并且访问某些站点会在那天晚上晚些时候在您的打印机托盘中产生惊喜。

You can bypass the print dialog with a browser plugin.

您可以使用浏览器插件绕过打印对话框。