js中打印指定区域内容 兼容IE FF Google等主流浏览器

2017-07-07 16:17:46

 

  • <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>

 

点赞

发表回复

电子邮件地址不会被公开。必填项已用 * 标注