Hide/show divs when printing to printer

烈酒焚心 提交于 2020-01-24 07:55:33

问题


Is it possible to click on a button/image/link on a page and include/exclude other elements from being printed? (on an actual printer) I'd like to give the users the selection of which elements to print. Is this feasible with jQuery of Javascript?

Edited for better understanding: I'd like to let the user choose which parts of the page he wants to print by adding a print this/don't print this button next to each div, overriding the default settings i've set in my print.css file.

Update: After Richard Neil Ilagan's suggestion, i've tried the following and I feel I'm close but cant nail it. Any suggestions?

<style type="text/css">
@media print { 
    .no-print { display:none; } 
}
</style>

<script>
$(document).ready(function() {
    $('.dontPrint').click(function() { $('maybe').addClass('no-print'); });
});
</script>

<img class="dontPrint" src="images/cancel.png" title="Dont print bla bla" /></a>
<div id="maybe">bla bla</div>

回答1:


Yes. You can do that by using CSS's @media print media type selector. Example (hide all inputs when printing):

@media print {
    input {
        display: none;
    }
}

As a design pattern, you may combine print media selector with the rest stylesheets to achieve toggle effect.

HTML:

<div class="for-screen">
    <input type="submit" value="Shown when not printing">
</div>
<div class="for-print">
    <p>Shown instead when printing</p>
</div>

CSS:

.for-screen {display: block;}
.for-print {display: none;}

@media print {
    .for-screen {display: none;}
    .for-print {display: block;}
}

More stuff on the matter:

  • Media types on CSS2 standard
  • CSS Design: Going to print (alistapart.com)



回答2:


you should set that with css.

media="print"

Try something like this:

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


来源:https://stackoverflow.com/questions/11086260/hide-show-divs-when-printing-to-printer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!