当前位置:网站首页>JS click the picture to print the image
JS click the picture to print the image
2022-07-26 21:26:00 【Ziwei front end】
Ctrl+P( or command+P stay macOS On ) To print web pages . Call the window.print() The function provides the same result of printing the entire page .iframe Call this function .print() Suppose the page has an image element and a print button , As shown below :<img id="image" src="/path/to/image.jpg" /> <button id="print">Print</button>click Deal with in the event : // Query the element
const printBtn = document.getElementById('print');
printBtn.addEventListener('click', function() {
...
});Create a fake 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);When iframe Insert image when ready
src Property defines the remote path , But we have to wait iframe Full load :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() Call immediately after loading the image iframe Functions on Windows : iframe.addEventListener('load', function() {
...
image.addEventListener('load', function() {
// Invoke the print when the image is ready
iframe.contentWindow.print();
});
});
Delete iframe
iframe.contentWindow.addEventListener('afterprint', function () {
iframe.parentNode.removeChild(iframe);
});边栏推荐
- 苹果官网罕见打折,iPhone13全系优惠600元;国际象棋机器人弄伤对弈儿童手指;国内Go语言爱好者发起新编程语言|极客头条
- Custom annotation (I)
- How to implement Devops with automation tools | including low code and Devops application practice
- Ros2 node communication realizes zero copy
- 拖放表格行
- Introduction to the billing function of wechat payment
- Set the template of core configuration file in idea
- Daily practice ----- there is a group of students' grades. Arrange them in descending order. To add a student's grade, insert it into the grade sequence and keep the descending order
- Test cases should never be used casually, recording the thinking caused by the exception of a test case
- Monitor MySQL based on MySQL exporter
猜你喜欢

记一次invalid bound statement xxxxxx 问题解决思路

IT系统为什么需要可观测性?

Summary of 4 years of software testing experience and interviews with more than 20 companies after job hopping

基于Hough变换的直线检测(Matlab)

golang版本管理gvm

Retrieve the parameters in this method in idea for our use -- 1. Class diagram. 2. Double click shift
![[MySQL series] - how much do you know about the index](/img/d7/5045a846580be106e2bf16d7b30581.png)
[MySQL series] - how much do you know about the index

GOM login configuration free version generate graphic tutorial

Sign up now: July 29 recommendation system summit 2022

The hardest lesson we learned from the crypto Market
随机推荐
There are six ways to help you deal with the simpledateformat class, which is not a thread safety problem
Deployment of kubernetes
MySQL -count: the difference between count (1), count (*), and count (column name)
JVM learning - memory structure - program counter & virtual machine stack & local method stack & heap & method area
golang版本管理gvm
Arm TZ hardware support
除了「加机器」,其实你的微服务还能这样优化
ECCV 2022 | complete four tracking tasks at the same time! Unicorn: towards the unification of target tracking
CONDA reports an error: json.decoder.jsondecodeerror:
调整表格列的大小
功能尝鲜 | 解密 Doris 复杂数据类型 ARRAY
Modify excel default code
[ffmpeg] add timestamp summary to video files
SPI配置
Registration conditions for information system project managers in the second half of 2022 (soft examination advanced)
DevSecOps,让速度和安全兼顾
[virtual machine data recovery] data recovery of XenServer virtual machine unavailable due to unexpected power failure
滤波及失真
Flutter Performance Optimization Practice - UI chapter
Niuke brush questions - MySQL series