JS打印页面指定区域

错误的写法:

复制代码
//打印
function printPage(areaId) {
    if (parent.$("#PrinFrame").length == 0) {
        parent.$("body").append('<iframe id="PrinFrame" style="display: none; "></iframe>');
    }

    var prinFrame = parent.$("#PrinFrame")[0];
    $(prinFrame).contents().find("body").html($("#" + areaId).html());

    var win = prinFrame.contentWindow;
    win.document.execCommand('Print');
}
View Code
复制代码

错误原因:只把打印区域的内容放到iframe中,样式信息丢了。

 

改进后的写法:

复制代码
//打印
function printPage(areaId) {
    if (parent.$("#PrinFrame").length == 0) {
        parent.$("body").append('<iframe id="PrinFrame" style="display: none; "></iframe>');
    }

    var prinFrame = parent.$("#PrinFrame")[0];
    var win = prinFrame.contentWindow;
    $(prinFrame).attr("src", window.location.href);
    $(prinFrame).load(function () {
        $(prinFrame).contents().find("body").html($("#" + areaId).html());
        win.document.execCommand('Print');
    });
}
View Code
复制代码

在iframe中重新加载当前页面,然后把body中的内容替换成待打印区域,这样iframe中保留了样式信息。

 

上面写法的缺点:多次点击打印按钮,iframe的load事件会被绑定多次;打印区域的大小超出A4纸范围;

再次改进后的写法:

复制代码
//打印
function printPage(areaId) {
    var prinFrame;
    var win;

    if (parent.$("#PrinFrame").length == 0) {
        parent.$("body").append('<iframe id="PrinFrame" style="display: none; "></iframe>');
        prinFrame = parent.$("#PrinFrame")[0];
        win = prinFrame.contentWindow;

        $(prinFrame).load(function () {
            setTimeout(function () {
                var html = '<table style="width:970px;"><tr><td>';
                html += $("#" + areaId).html();
                html += '</td></tr></table>';
                $(prinFrame).contents().find("body").html(html);
                win.document.execCommand('Print');
            }, 100);
        });
    }
    else {
        prinFrame = parent.$("#PrinFrame")[0];
    }

    $(prinFrame).attr("src", window.location.href);
}
View Code
复制代码

再次改进后,确保iframe的load事件只被绑定一次;用宽度为970的table限制打印区域大小。

 

上面的写法还是有错误,重新打开tab页时,点击打印,不再进入iframe的load方法,再修改:

复制代码
//打印
function printPage(areaId) {
    if (parent.$("#PrinFrame").length == 0) {
        parent.$("body").append('<iframe id="PrinFrame" style="display: none; "></iframe>');
    }

    parent.$("#PrinFrame").attr("src", window.location.href);

    parent.$("#PrinFrame").one("load", function () {
        setTimeout(function () {
            var html = '<table style="width:970px;"><tr><td>';
            html += $("#" + areaId).html();
            html += '</td></tr></table>';
            parent.$("#PrinFrame").contents().find("body").html(html);
            parent.$("#PrinFrame")[0].contentWindow.document.execCommand('Print');
        }, 100);
    });
}
View Code
复制代码

 

弄了一天,分页打印的时候还是有问题,如下图:

 

posted @   0611163  阅读(1139)  评论(0编辑  收藏  举报
编辑推荐:
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
· .NET 适配 HarmonyOS 进展
阅读排行:
· 手把手教你更优雅的享受 DeepSeek
· 腾讯元宝接入 DeepSeek R1 模型,支持深度思考 + 联网搜索,好用不卡机!
· AI工具推荐:领先的开源 AI 代码助手——Continue
· 探秘Transformer系列之(2)---总体架构
· V-Control:一个基于 .NET MAUI 的开箱即用的UI组件库
点击右上角即可分享
微信分享提示