简体   繁体   中英

Clear webpage after printing

I am trying to print the content of a particular DIV and after printing the DIV the webpage has to be fixed properly but its just displaying only the DIV I just printed and hides the actual webpage. Any guess what's wrong?

My code is

function printdiv(printpage)
{
var newstr = document.all.item(printpage).innerHTML;
document.body.innerHTML = newstr;
window.print();
window.close();
}

and

<img src="im/print.png" onClick="printdiv('mytabprint');" width="50" />

In the above code mytabprint is the DIV element which I want to print.

Any guess what's wrong?

Yes, you're not "hiding" the other contents, you're actually replacing the whole page contents with your div:

document.body.innerHTML = newstr;

You can simply use a print stylesheet instead. For example, add this to your CSS:

@media print {
   /* 
   rules that only apply for printing 
   like hiding some content
   */
}

Or link a separate CSS for printing with

<link rel="stylesheet" href="your_css.css" type="text/css" media="print" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM