web建站教程
  1. 首页
  2. vuejs
  3. js
  4. 好玩
  5. seo教程
  6. 前端知识
  7. 百度echarts
  8. 更多
    php入门
    nodejs
    mockjs
    reactjs
    mysql
    wordpress
    织梦cms
    帝国cms
    git教程
    IT知识
    模板大全
    休息站

js实现局部分页打印

492 ℃
     

1、先引入print.js

(function ($) {
    var printAreaCount = 0;
    $.fn.printArea = function () {
        var ele = $(this);
        var idPrefix = "printArea_";
        removePrintArea(idPrefix + printAreaCount);
        printAreaCount++;
        var iframeId = idPrefix + printAreaCount;
        var iframeStyle = 'position:absolute;width:0px;height:0px;left:-500px;top:-500px;';
        iframe = document.createElement('IFRAME');
        $(iframe).attr({
            style: iframeStyle,
            id: iframeId
        });
        document.body.appendChild(iframe);
        var doc = iframe.contentWindow.document;
        $(document).find("link").filter(function () {
            return $(this).attr("rel").toLowerCase() == "stylesheet";
        }).each(
                function () {
                    doc.write('<link type="text/css" rel="stylesheet" href="'
                            + $(this).attr("href") + '" >');
                });
        doc.write('<div class="' + $(ele).attr("class") + '">' + $(ele).html()
                + '</div>');
        doc.close();
        var frameWindow = iframe.contentWindow;
        frameWindow.close();
        frameWindow.focus();
        frameWindow.print();
    }
    var removePrintArea = function (id) {
        $("iframe#" + id).remove();
    };
})(jQuery);

2、写入html代码,以table表格当分页,table里的style不能删掉,不然实现不了分页

<a href="javascript:doPrint();">打印</a>
<div id="print">
   <table style="page-break-after:always">打印1</table>
   <table style="page-break-after:always">打印2</table>
   <table style="page-break-after:always">打印3</table>
</div>

3、引入jquery,再写入以下方法

function doPrint() {
   $("#print").printArea();
}

wordpress文章nextpage分页最后添加“下一页”按钮

Chrome浏览器点击打印不预览直接打印(设置流程)

jquery利用printArea实现局部打印和全局打印

织梦cms模板分页列表显示前面和最后,中间用省略号

vuejs实现部分页面跳转后禁止返回到前一个页面

标签: 分页, 打印

上面是“js实现局部分页打印”的全面内容,想了解更多关于 js 内容,请继续关注web建站教程。

当前网址:https://ipkd.cn/webs_131.html

声明:本站提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请发送到邮箱:admin@ipkd.cn,我们会在看到邮件的第一时间内为您处理!

当前位置: 网站首页 > js
本文共计1433个字,预计阅读时长10分钟
生活小工具,收录了80多款小工具
上一篇: 推荐一个免费可商用psd文件素材网站——FreePik
下一篇: 一行代码轻松实现优雅的过渡动画插件——AutoAnimate
x 打工人ai神器