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

jquery利用jqprint实现局部打印(兼容IE8)

864 ℃
     

jquery如何利用jqprint插件实现局部打印和全局打印,下面web建站小编给大家详细介绍一下实现方法!

jqprint插件代码

(function($) {
  var opt;
  
  $.fn.jqprint = function (options) {
    opt = $.extend({}, $.fn.jqprint.defaults, options);
  
    var $element = (this instanceof jQuery) ? this : $(this);
    
    if (opt.operaSupport && $.browser.opera) 
    { 
      var tab = window.open("","jqPrint-preview");
      tab.document.open();
  
      var doc = tab.document;
    }
    else 
    {
      var $iframe = $("<iframe  />");
    
      if (!opt.debug) { $iframe.css({ position: "absolute", width: "0px", height: "0px", left: "-600px", top: "-600px" }); }
  
      $iframe.appendTo("body");
      var doc = $iframe[0].contentWindow.document;
    }
    
    if (opt.importCSS)
    {
      if ($("link[media=print]").length > 0) 
      {
        $("link[media=print]").each( function() {
            doc.write("<link type='text/css' rel='stylesheet' href='" + $(this).attr("href") + "' media='print' />");
        });
      }
      else 
      {
        $("link").each( function() {
            doc.write("<link type='text/css' rel='stylesheet' href='" + $(this).attr("href") + "' />");
        });
      }
    }
    
    if (opt.printContainer) { doc.write($element.outer()); }
    else { $element.each( function() { doc.write($(this).html()); }); }
    
    doc.close();
    
    (opt.operaSupport && $.browser.opera ? tab : $iframe[0].contentWindow).focus();
    setTimeout( function() { (opt.operaSupport && $.browser.opera ? tab : $iframe[0].contentWindow).print(); if (tab) { tab.close(); } }, 1000);
  }
  
  $.fn.jqprint.defaults = {
  	debug: false,
  	importCSS: true, 
  	printContainer: true,
  	operaSupport: true
  };
  
  jQuery.fn.outer = function() {
    return $($('<div></div>').html(this.clone())).html();
  } 
})(jQuery);

调用方法:

function print(){
  $("#div").jqprint(); //需要打印的div
}

阻止事件冒泡e.stopPropagation()不兼容ie6/7/8解决方法

阻止默认行为ev.preventDefault()不兼容ie6/7/8解决方法

classList.add和classList.remove不兼容ie6/7/8/9解决方法

自定义属性data-* 不兼容ie6/7/8/9/10解决方法

getElementsByclassName不兼容IE6/7/8解决方法!

标签: jqprint, jquery, 兼容IE8, 局部打印

上面是“jquery利用jqprint实现局部打印(兼容IE8)”的全面内容,想了解更多关于 js 内容,请继续关注web建站教程。

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

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

当前位置: 网站首页 > js
本文共计1398个字,预计阅读时长10分钟
生活小工具,收录了80多款小工具
上一篇: 推荐一款由字制区设计团队的免费综艺字体——字制区喜脉体
下一篇: 推荐一款免费开源的跨平台MarkDown编辑器——Mark Text
x 打工人ai神器