admin管理员组

文章数量:1346689

I've got a webpage that utilises bPopUp to pop up a modal window with jQuery.

The contents of that window are exactly what I would like to print (and nothing else on the main page).

I've tried linking a button on the popup window to run the mand window.print(); but this just prints the whole page showing the popup window obscuring the rest of the page.

Does anyone know how I can only print the contents of the pop up window and nothing else?

Thanks

I've got a webpage that utilises bPopUp to pop up a modal window with jQuery.

The contents of that window are exactly what I would like to print (and nothing else on the main page).

I've tried linking a button on the popup window to run the mand window.print(); but this just prints the whole page showing the popup window obscuring the rest of the page.

Does anyone know how I can only print the contents of the pop up window and nothing else?

Thanks

Share Improve this question asked Oct 25, 2016 at 13:32 John TJohn T 1,0783 gold badges14 silver badges29 bronze badges 3
  • 2 console.log($("#popup").html()) – Sulthan Allaudeen Commented Oct 25, 2016 at 13:34
  • @SulthanAllaudeen apologies for my ignorance, but will this send the contents to the printer? Or just to the 'console' at the bottom of the page? Thanks – John T Commented Oct 25, 2016 at 13:36
  • jQuery html(): Gets the HTML contents of the first element in the set of matched elements or set the HTML contents of every matched element.. See more: api.jquery./html – Yosvel Quintero Commented Oct 25, 2016 at 13:38
Add a ment  | 

1 Answer 1

Reset to default 7

Sample HTMP

<div id='DivIdToPrint'>
    <p>This is a Popup which needs to be printed.</p>
</div>

<div>
    <p>Do not print.</p>
</div>

<input type='button' id='btn' value='Print' onclick='printDiv();'>

Javascript

function printDiv() 
{
var divToPrint=document.getElementById('DivIdToPrint');
var newWin=window.open('','Print-Window');
newWin.document.open();
newWin.document.write('<html><body onload="window.print()">'+divToPrint.innerHTML+'</body></html>');
newWin.document.close();
setTimeout(function(){newWin.close();},10);
}

Give an ID to your Popup div and use above example. You need to give inline styling to print the popup exactly.

本文标签: jqueryHow to print the contents of a popup window with javascriptStack Overflow