当前位置:网站首页>js点击图片打印图像
js点击图片打印图像
2022-07-26 20:31:00 【紫微前端】
Ctrl+P(或command+P在 macOS 上)来打印网页。调用该window.print()函数提供了打印整个页面的相同结果。iframe调用该函数。print()假设页面有一个图像元素和一个打印按钮,如下所示:<img id="image" src="/path/to/image.jpg" /> <button id="print">Print</button>click事件中处理: // Query the element
const printBtn = document.getElementById('print');
printBtn.addEventListener('click', function() {
...
});创建一个假的 iframe
// Create a fake iframe
const iframe = document.createElement('iframe');
// Make it hidden
iframe.style.height = 0;
iframe.style.visibility = 'hidden';
iframe.style.width = 0;
// Set the iframe's source
iframe.setAttribute('srcdoc', '<html><body></body></html>');
document.body.appendChild(iframe);当 iframe 准备好时插入图像
src属性定义的远程路径,但我们必须等待 iframe 完全加载:iframe.addEventListener('load', function () {
// Clone the image
const image = document.getElementById('image').cloneNode();
image.style.maxWidth = '100%';
// Append the image to the iframe's body
const body = iframe.contentDocument.body;
body.style.textAlign = 'center';
body.appendChild(image);
});print()加载图像后立即调用 iframe 窗口上的函数: iframe.addEventListener('load', function() {
...
image.addEventListener('load', function() {
// Invoke the print when the image is ready
iframe.contentWindow.print();
});
});
删除 iframe
iframe.contentWindow.addEventListener('afterprint', function () {
iframe.parentNode.removeChild(iframe);
});边栏推荐
- Flash source code outline
- What are the characteristics of low code tools? The two development tracks of low code that can be seen by discerning people!
- Ros2 method of obtaining current system time
- DeepFake捏脸真假难辨,汤姆·克鲁斯比本人还像本人!
- js中join方法
- SPI配置
- Svn uses fragmented ideas
- 和月薪3W的字节程序员聊过后,才知道自己一直在打杂...
- idea中设置核心配置文件的模板
- conda报错:json.decoder.JSONDecodeError:
猜你喜欢

微服务化解决文库下载业务问题实践

golang版本管理gvm

手机\固定电话座机呼叫转移设置方法

Mobile phone \ landline call forwarding setting method
![[hero planet July training leetcode problem solving daily] 26th and check the collection](/img/f1/e63b1f35b883274ca077cbd2ab4c24.png)
[hero planet July training leetcode problem solving daily] 26th and check the collection

一种用于实体关系抽取的统一标签空间

JVM学习----内存结构----程序计数器&虚拟机栈&本地方法栈&堆&方法区

Flutter Performance Optimization Practice - UI chapter

如何在一个项目中使用多种不同的语言?
![[download materials of harmoniyos topics] HDD Hangzhou station · offline salon focuses on application innovation to show the ecological charm of Hongmeng](/img/62/9e2ff0dc2c8b049fd32ad56334a0c0.jpg)
[download materials of harmoniyos topics] HDD Hangzhou station · offline salon focuses on application innovation to show the ecological charm of Hongmeng
随机推荐
牛客刷题——Mysql系列
Relevant contents about wireless communication
Redis面试题
Flutter Performance Optimization Practice - UI chapter
我们从Crypto市场中学到的最艰难一课
力扣每日一题-第43天-168. Excel表列名称
Valley segment coverage - (summary of interval sequencing problem)
1-《PyTorch深度学习实践》-线性模型
Object.getOwnPropertyNames() VS Object.keys()
游览器——游览器游览器缓存
Alkbh1
Browser browser cache
记一次invalid bound statement xxxxxx 问题解决思路
The hardest lesson we learned from the crypto Market
2022-7-26 the seventh group of abstractions and interfaces
【Oracle实训】-部署号称零停机迁移的OGG
【HCIE安全】双机热备-主备备份
2022 open atom global open source summit agenda express | list of sub forum agenda on July 27
Svn uses fragmented ideas
【HarmonyOS议题资料下载】HDD杭州站·线下沙龙专注应用创新 展现鸿蒙生态魅力