Print
only?

前端 未结 30 2028
暖寄归人
暖寄归人 2020-11-22 00:25

How do I print the indicated div (without manually disabling all other content on the page)?

I want to avoid a new preview dialog, so creating a new window with this

30条回答
  •  执笔经年
    2020-11-22 00:30

    One more approch without affecting current page and it also persist the css while printing. Here selector must be specific div selector which content we need to print.

    printWindow(selector, title) {
       var divContents = $(selector).html();
       var $cssLink = $('link');
       var printWindow = window.open('', '', 'height=' + window.outerHeight * 0.6 + ', width=' + window.outerWidth  * 0.6);
       printWindow.document.write('

    ' + title + '

    '); for(var i = 0; i<$cssLink.length; i++) { printWindow.document.write($cssLink[i].outerHTML); } printWindow.document.write(''); printWindow.document.write(divContents); printWindow.document.write(''); printWindow.document.close(); printWindow.onload = function () { printWindow.focus(); setTimeout( function () { printWindow.print(); printWindow.close(); }, 100); } }

    Here provided some time out show that external css get applied to it.

提交回复
热议问题