Html打印需要内容块(Js实现)

首先在head里面加入下面一段js代码:

function preview(oper) {
    if (oper < 10) {
        bdhtml = window.document.body.innerHTML;
        //获取当前页的html代码
        sprnstr = "<!--startprint" + oper + "-->";
        //设置打印开始区域
        eprnstr = "<!--endprint" + oper + "-->";
        //设置打印结束区域
        prnhtml = bdhtml.substring(bdhtml.indexOf(sprnstr) + 18);
        //从开始代码向后取html
        prnhtml = prnhtml.substring(0, prnhtml.indexOf(eprnstr));
        //从结束代码向前取html
        window.document.body.innerHTML = prnhtml;
        window.print();
        window.document.body.innerHTML = bdhtml;
    } else {
        window.print();
    }
};

然后在所需要打印的代码,用<!--startprint1-->和<!--endprint1-->包围着,如下:

<!--startprint1-->
<!--打印内容开始-->
<div id=sty>
    ...
</div>
<!--打印内容结束-->
<!--endprint1-->

最后加上一个打印的按钮

<input type=button name='button_export' title='打印' οnclick=preview(1) value=打印>
另外说明一下,在一个HTML页面里面,可以设置多个打印区域,需要改动一下的就只是几个数字就OK了。如:
在选择第二个区域里面时用<!--startprint2--><!--endprint2-->包围着,而按钮自然也改成对应的preview(2)了。这样第二区域的打印就完成。
 
还有一点,就是CSS样式表的问题了,打印的效果是不包含背景的打印的,设置是注意一下。<style media="print">、<link media="print">的用法合理应用,media="print"是不被网页所显示的,只能在打印的效果上存在,可以设置出打印效果和在网页上所显示的不一样。

转载于:https://www.cnblogs.com/Violety/p/9446767.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现方法如下: 1. 单页打印 HTML 代码: ```html <div id="print-content"> <h1>打印内容</h1> <p>这是需要打印内容。</p> </div> <button onclick="printContent()">打印</button> ``` JavaScript 代码: ```javascript function printContent() { var content = document.getElementById("print-content").innerHTML; var printWindow = window.open('', '', 'height=400,width=800'); printWindow.document.write('<html><head><title>打印内容</title>'); printWindow.document.write('</head><body >'); printWindow.document.write(content); printWindow.document.write('</body></html>'); printWindow.document.close(); printWindow.print(); } ``` 2. 批量打印 HTML 代码: ```html <div class="print-container"> <div class="print-item"> <h1>打印内容1</h1> <p>这是需要打印内容。</p> </div> <div class="print-item"> <h1>打印内容2</h1> <p>这是需要打印内容。</p> </div> <div class="print-item"> <h1>打印内容3</h1> <p>这是需要打印内容。</p> </div> </div> <button onclick="printAll()">打印</button> ``` JavaScript 代码: ```javascript function printAll() { var printContainer = document.getElementsByClassName('print-container')[0]; var printItems = printContainer.getElementsByClassName('print-item'); for (var i = 0; i < printItems.length; i++) { var content = printItems[i].innerHTML; var printWindow = window.open('', '', 'height=400,width=800'); printWindow.document.write('<html><head><title>打印内容</title>'); printWindow.document.write('</head><body >'); printWindow.document.write(content); printWindow.document.write('</body></html>'); printWindow.document.close(); printWindow.print(); } } ``` 以上代码实现了在页面中批量打印多个内容。每个内容需要有一个带有 `print-item` 类名的父元素包裹,然后通过 `getElementsByClassName` 获取到所有需要打印内容,循环遍历进行打印
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值