要实现点击按钮后打印指定div区域,可以使用JavaScript中的window.print()函数。以下是一个简单的实现示例:
HTML部分:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Print Div Example</title> </head> <body> <div id="printSection"> <!-- 这里是你想打印的内容 --> <p>这部分内容将被打印。</p> </div> <button onclick="printDiv()">打印</button> <script> function printDiv() { var printContents = document.getElementById('printSection').innerHTML; var originalContents = document.body.innerHTML; document.body.innerHTML = printContents; window.print(); document.body.innerHTML = originalContents; } </script> </body> </html>
扫描二维码手机查看该文章
文章引用:https://www.qinghuahulian.com/news/webzhishi/1461.html