jquery + iframe 实现打印功能

使用 window.print() 和 iframe.print() 实现html自定义页面打印功能 

引入jquery文件:<script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>

  1. 使用window.print()打印 
<body>
    <button id="printBtn">打印</button>
</body>
<script>
    $('#printBtn').click(function () {
        //定义头部样式
        var f = ["<head>", "<style>", "body{font-size: 12px; color: #666;}",
            "table{width: 100%; border-collapse: collapse; border-spacing: 0;}",
            "th,td{width:15%;line-height: 20px; border: 1px solid #ccc; text-align: center;}",
            "</style>", "</head>"].join("");
        //绘制打印页面的内容
        var v = document.createElement("div");
        var allData = [
            { username: '张三', email: '123456@asa.com', experience: '44', sex: '女' },
            { username: '李四', email: '214325@asa.com', experience: '200', sex: '男' },
            { username: '王五', email: '354534@asa.com', experience: '0', sex: '男' },
            { username: '汤六', email: '124432@asa.com', experience: '12', sex: '女' }
        ]
        //绘制自定义表格版面
        var recordTable = "<table border='1' cellspacing='0'><thead><th>用户名</th><th>邮箱</th><th>积分</th><th>性别</th></thead>";
        for (let i = 0; i < allData.length; i++) {
            let data = allData[i]
            recordTable += `<tr>
                                <td>${data.username}</td>
                                <td>${data.email}</td>
                                <td>${data.experience}</td>
                                <td>${data.sex}</td>
                            </tr>`;
        }
        recordTable += "</table>";
        //recordTable为已拼凑好的内容
        $(v).append(recordTable)

        // 新开窗口进行打印
        var h = window.open("打印窗口", "_blank");
        //拼接头部样式和打印页面内容
        h.document.write(f + $(v).prop("outerHTML"));
        h.document.close();
        h.print();
        h.close();
    })

</script>

 2.使用iframe在本页面进行打印


<body>
    <button id="printBtn">打印</button>
</body>
<script>
    $('#printBtn').click(function () {
        //定义头部样式
        var f = ["<head>", "<style>", "body{font-size: 12px; color: #666;}",
            "table{width: 100%; border-collapse: collapse; border-spacing: 0;}",
            "th,td{width:15%;line-height: 20px; border: 1px solid #ccc; text-align: center;}",
            "</style>", "</head>"].join("");
        //绘制打印页面的内容
        var v = document.createElement("div");
        var allData = [
            { username: '张三', email: '123456@asa.com', experience: '44', sex: '女' },
            { username: '李四', email: '214325@asa.com', experience: '200', sex: '男' },
            { username: '王五', email: '354534@asa.com', experience: '0', sex: '男' },
            { username: '汤六', email: '124432@asa.com', experience: '12', sex: '女' }
        ]
        //绘制自定义表格版面
        var recordTable = "<table border='1' cellspacing='0'><thead><th>用户名</th><th>邮箱</th><th>积分</th><th>性别</th></thead>";
        for (let i = 0; i < allData.length; i++) {
            let data = allData[i]
            recordTable += `<tr>
                                <td>${data.username}</td>
                                <td>${data.email}</td>
                                <td>${data.experience}</td>
                                <td>${data.sex}</td>
                            </tr>`;
        }
        recordTable += "</table>";
        //recordTable为已拼凑好的内容
        $(v).append(recordTable)

        const iframe = document.createElement('iframe');
        iframe.style = "position: absolute;top: 0;left: 0;z-index:-1;display:none";
        let doc = null;
        iframe.id = 'printIframe';
        document.body.appendChild(iframe);
        doc = iframe.contentWindow.document;

        //注意这里拼接样式和页面内容
        doc.write(f + $(v).prop("outerHTML"));
        doc.close();

        // 获取iframe的焦点,从iframe开始打印
        iframe.contentWindow.focus();
        iframe.contentWindow.print();

        //打印完毕后移除iframe
        iframe.parentNode.removeChild(iframe);
    })

</script>
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值