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

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

1327 ℃

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分钟
Trae:新一代免费的AI编程工具

生活小工具

收录了万年历、老黄历、八字智能排盘等100+款小工具!生活小工具
上一篇: 推荐一个可以查询老黄历的网站,可以根据五行取名!
下一篇: 详细介绍HTTP状态码不同之处的原因(什么原因导致的)
x 打工人ai神器