Javascript 图像src未写入弹出窗口正文?
我需要使用JavaScript打印图像。我找不到解决此问题的jQuery解决方案,因此我尝试了以下方法:Javascript 图像src未写入弹出窗口正文?,javascript,jquery,image,Javascript,Jquery,Image,我需要使用JavaScript打印图像。我找不到解决此问题的jQuery解决方案,因此我尝试了以下方法: var printWindow = window.open('', 'Print Image', 'height=400,width=400'); printWindow.document.write('<html><head><title>Print Image</title></head><body></bod
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();
var printWindow=window.open(“”,'Print Image','height=400,width=400');
printWindow.document.write('Print Image');
var img=printWindow.document.createElement('image');
img.src=http://ecx.images-amazon.com/images/I/51j68MN%2B99L._SL500_SS100_.jpg';
img.onload=函数(){
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()
函数中放置了图像标记,但我发现有些图像的打印窗口是空白的。我的理论是,打印窗口在一些图像完成下载之前打开,因此显示(和打印)空白。这就是我为什么尝试这种方法的原因
为什么这不起作用,如何修复它?你试过了吗
var img = new Image();
而不是
var img = printWindow.document.createElement('image');
不是正确的图像标记(
是吗)
使用正确的标记名
查看您在加载图像之前附加的图像。此外,在图像加载和打印(最后两行)之前关闭
print窗口
img
是正确的标签名称:
var img = printWindow.document.createElement('img');
img.src = 'http://ecx.images-amazon.com/images/I/51j68MN%2B99L._SL500_SS100_.jpg';
img.onload = function() {
printWindow.document.body.appendChild(img);
printWindow.print();
printWindow.close();
};
//printWindow.document.body.appendChild(img);
//printWindow.document.close();
您没有附加img。。。
var img = printWindow.document.createElement('img');
img.src = 'http://ecx.images-amazon.com/images/I/51j68MN%2B99L._SL500_SS100_.jpg';
img.onload = function() {
printWindow.document.body.appendChild(img);
printWindow.print();
printWindow.close();
};
//printWindow.document.body.appendChild(img);
//printWindow.document.close();