打印按钮以打印附加图像 [英] Print button to print a attached image
本文介绍了打印按钮以打印附加图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在我的网页中添加一个打印按钮.点击打印图标后,它应该会自动开始打印附加的图像
I would like to add a print button in my webpage. upon clicking the print icon, it should automatically start print the attached image
我怎样才能做到这一点?
How can i achieve this?
推荐答案
- 使用
window.open
打开新窗口. - 写入
img
标签. - 加载图像后,使用
window.print()
打印文档. - 使用
window.close()
关闭窗口
- Open new window using
window.open
. - Write to it the
img
tag. - Once the image was loaded, print the document using
window.print()
. - Close the window using
window.close()
你可以用这样的代码来做到这一点:
You can do this with code like this:
function printImg(url) {
var win = window.open('');
win.document.write('<img src="' + url + '" onload="window.print();window.close()" />');
win.focus();
}
<img src="http://i.stack.imgur.com/hCYTd.jpg" />
<button onclick="printImg('http://i.stack.imgur.com/hCYTd.jpg')">Print</button>
http://jsbin.com/qaruze/edit?html,js
这篇关于打印按钮以打印附加图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文