简体   繁体   English

如何使用javascript打印页面并隐藏标题?

[英]How do I print a page using javascript and hide headers?

I have a page on my website called printUsers.html. 我的网站上有一个名为printUsers.html的页面。
On it there is a button which prints the page using 'javascript:window.print()'. 它上面有一个按钮,该按钮使用'javascript:window.print()'打印页面。
I am also using '@media print' on the page to hide some buttons when the page is printed. 我还在页面上使用“ @media print”来隐藏页面打印时的一些按钮。
This is all working well and I have no problems here. 一切都很好,我在这里没有问题。

The problem is the following: 问题如下:
All the pages on the site, extend from the main page. 该站点上的所有页面均从主页开始。 So at the top of printUsers.html I have: 因此,在printUsers.html的顶部,我有:
#{extends 'App/main.html' /}

This includes styles and a header which has buttons and drop-downs. 这包括样式和带有按钮和下拉菜单的标题。
When the user clicks the print button, I want to hide all the header and buttons etc, which come from the main.html. 当用户单击打印按钮时,我想隐藏所有来自main.html的标题和按钮等。
I tried wrapping it into a div, giving it an id and hiding it but this didn't work. 我试着将它包装到div中,给它一个id并隐藏它,但这没有用。

I have just started using javascript so any help would be much appreciated. 我刚刚开始使用javascript,因此任何帮助将不胜感激。
Thanks. 谢谢。

The CSS way CSS方式

@media print @媒体印刷

Use @media Print as you stated in your question, but include all the elements you don't want to see in your printed result, and put display:none to them. 按照问题中的说明使用@media Print ,但要在打印结果中包含所有不希望看到的元素,然后将display:none放在其中。 You can also apply some margin:0 auto; text-align:center; 您也可以套用margin:0 auto; text-align:center; margin:0 auto; text-align:center; to your main content if you want to center it into your page. 如果您想将其居中放置在页面中。

Edit: You can hide any element, such as header this way: 编辑:您可以通过以下方式隐藏任何元素,例如header

header
{
    display:none;
}

footer
{
    display:none;
}

The Javascript way Javascript方式

Button's onClick 按钮的onClick

Your button's onclick: 您的按钮的onclick:

Button onClick() 按钮onClick()

<button id="printThatText" name="printThatText" onclick="printPage();">Print this page</button>"

Your javascript code in the header (or at the end of the page) 标头(或页面末尾)中的JavaScript代码

Javascript 使用Javascript

function printPage()
{
    var myDropDown = document.getElementById('myDropDown');
    myDropDown.style.display = "none";
    //Whatever other elements to hide.
    window.print();
    myDropDown.style.display = "block";
    return true;
}

You could also put all of these elements in an array and make a for ... in ... loop to show/hide them. 您也可以将所有这些元素放入数组中,并在for ... in ...循环中显示/隐藏它们。

Wrap the contents with an element that encapsulates all of stuff you want to hide. 用一个元素包装内容,该元素封装了您要隐藏的所有内容。 In the print CSS, set the display to none. 在打印CSS中,将显示设置为无。

The CSS: CSS:

@media print {
    #myHeader, #myFooter { display: none }
}

The HTML: HTML:

<div id="myHeader">
  <ul>
    <li>
      <a>My link</a>
   </li>
 </ul>
</div>
<div id="myContent">
  <p>This will print fine</p>
</div>
<div id="myFooter">
  <p>This will not print</p>
</div>

You could always use HTML5 header/footer elements! 您始终可以使用HTML5页眉/页脚元素!

也许尝试相反的做法 -仅打印特定的div-而不是隐藏其他div:仅打印<div id = printarea> </ div>?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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