我需要使用 JavaScript 打印图像。我找不到 jQuery 解决方案,所以我尝试了以下方法:
var printWindow = window.open('', 'Print Image', 'height=400,width=400');
printWindow.document.write('<html><head><title>Print Image</title></head><body></body></html>');
var img = printWindow.document.createElement('image');
img.src = 'http://ecx.images-amazon.com/images/I/51j68MN%2B99L._SL500_SS100_.jpg';
img.onload = function() {
printWindow.print();
printWindow.close();
};
printWindow.document.body.appendChild(img);
printWindow.document.close();
不幸的是,弹出的 HTML 是这样的:
<html><head><title>Print Image</title></head><body><image></body></html>
因此看来没有为图像设置 src
属性。
我之前已将图像标签放入 document.write()
函数中,但我发现某些图像的打印窗口是空白的。我的理论是,打印窗口在某些图像完成下载之前打开,因此显示(并打印)空白。这就是我尝试这个方法的原因。
为什么这不起作用,如何修复?
最佳答案
关于javascript - 图像 src 未写入弹出窗口主体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25261453/