我正在尝试打印 ASP.NET Image
使用示例 here .
当我将上面的示例用于 div 时,它可以工作,但我无法将其调整为打印任何其他内容。我怎样才能让它打印 Image
?我要包Image
吗?在 div 中并将脚本中的“.text()”部分更改为其他内容?
最佳答案
因为您将错误的参数传递给打印函数。在 JavaScript 中打印一些东西就像调用 window.print();
一样简单方法。要测试它,只需使用 开发者工具并写入其控制台:
window.print();
现在,当你想打印一些特定的东西时,你有两种方法:
现在,您可以做的是编写自己的函数:
function printImage(image)
{
var printWindow = window.open('', 'Print Window','height=400,width=600');
printWindow.document.write('<html><head><title>Print Window</title>');
printWindow.document.write('</head><body ><img src=\'');
printWindow.document.write(image.src);
printWindow.document.write('\' /></body></html>');
printWindow.document.close();
printWindow.print();
}
var image = document.getElementById('image');
printImage(image);
你也可以看到这个函数在运行 here .
就让浏览器打开弹窗,还要注意我只传了
src
图像元素的值到新窗口。
关于javascript - 如何使用javascript打印图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8443758/