代码之家  ›  专栏  ›  技术社区  ›  Pearl

使用jQuery插件时,整个页面都在IE中打印

  •  0
  • Pearl  · 技术社区  · 11 年前

    HTML格式

    <div id="printDiv">
    <!-- content Here -->
    </div>
    <input type="button" value="Print" onclick="printDiv()"/>
    

    JQuery(JQuery)

    function printDiv()
    {
      $("#printDiv").print();
    }
    

    这是在使用Mozilla Firefox时使用特定div,但在使用IE(版本10)时,它会打印整个页面。

    我正在使用这个jQuery插件。

    jQuery.print.js
    

    请帮帮我。

    1 回复  |  直到 11 年前
        1
  •  4
  •   Abhidev    11 年前

    你可以在没有插件的情况下完成。我不知道这个插件是否在做一些额外的事情。您必须编写一个自定义的js函数来只打印div,而不打印整个页面。。。

    html格式

    <div id="printDiv">
           //content Here
     </div>
    

    js公司

    function customPrint(id) {
         var cont = $('#'+id).html(),
             pageCont = $('body').children();// Updated
    
         /*Replace the page content with the div content that needs to be printed*/
         $('body').html(cont);
    
         /*Print function call*/
         window.print();
    
         /*Place the original page content back*/
         $('body').html(pageCont);
    }
    
    customPrint("printDiv");
    

    使现代化

    function customPrint(id) {
             var cont = $('#'+id),
                 pageCont = $('body');
    
             /*Hide the page content*/
             pageCont.hide();
             cont.show();
    
             /*Print function call*/
             window.print();
    
             /*Display back the page content*/
             pageCont.show();
        }