0

HTML

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

jQuery

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

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

我正在使用这个 jQuery 插件。

jQuery.print.js

请帮我。

4

1 回答 1

4

你可以在没有插件的情况下做到这一点。我不知道那个插件是否在做额外的事情。您将不得不编写一个自定义 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();
    }
于 2013-09-26T10:25:30.853 回答