js 打印DIV内容 print div 兼容IE FF Google等主流浏览器

1.js中打印指定区域内容  兼容IE FF Google等主流浏览器
<script type="text/javascript">
function wesPrint(id_s)
  {
    var cc = document.getElementById(id_s).innerHTML;
    var isIe=0;
    if(navigator.userAgent.indexOf('MSIE')>0){
      isIe = 1;
    }
    var frame = document.getElementById('dsh_myframe');
    if (!frame) {
      if (isIe) {
        frame = document.createElement('<iframe id = "dsh_myframe"></iframe>');
      } else {
        frame = document.createElement('iframe');
        frame.id ='dsh_myframe';
        frame.setAttribute('style','width: 0pt; height: 0pt;')
      }
    }
    if (isIe) {
      frame.src = 'javascript:;';
      frame.style.cssText= 'width: 0pt; height: 0pt;';
    }
    document.body.appendChild(frame);
    if (isIe) {
      doc = frame.contentWindow.document;
    } else {
      doc = frame.contentDocument;
    }
    doc.write(cc);
    doc.close();
    frame.contentWindow.focus();
    if(isIe){
      setTimeout(function(){
        frame.contentWindow.print();
      },2);
    }else{
      frame.contentWindow.print();
    }
  }
</script>

<div id="div1">Print  area.....Test</div>
<button onlick="wesPrint('div1');">Print Div</button>


评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值