Javascript 如何使用代码删除打印页眉/页脚
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7710315/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to remove print header/footer with code
提问by Beginner
I am using the following code to print a page within my application...
我正在使用以下代码在我的应用程序中打印页面...
<html><body onload=""window.print();"">"
sHtmlBody = sHtmlBody & "<body>"
The window.print()
is working fine.
I know once the print comes up I can manually go into the settings and remove headers and footer.
On IE I know that I have to go to print preview and then remove the print headers.
该window.print()
工作正常。我知道一旦打印出来,我就可以手动进入设置并删除页眉和页脚。在 IE 上,我知道我必须去打印预览,然后删除打印标题。
However, is there some line of code which does this automatically so the users of the application don't have to do this?
但是,是否有一些代码行可以自动执行此操作,以便应用程序的用户不必执行此操作?
EDIT:
编辑:
sHtmlBody = "<style type='text/css'>"
sHtmlBody = sHtmlBody & " @media print{"
sHtmlBody = sHtmlBody & " body{ background-color:#FFFFFF; background-image:none; color:#000000 }"
sHtmlBody = sHtmlBody & " #ad{ display:none;}"
sHtmlBody = sHtmlBody & " #leftbar{ display:none;}"
sHtmlBody = sHtmlBody & " #contentarea{ width:100%;}"
sHtmlBody = sHtmlBody & " }"
sHtmlBody = sHtmlBody & " </style>"
sHtmlBody = sHtmlBody & "<html><body onload=""window.print();"">"
sHtmlBody = sHtmlBody & "<body>"
回答by Pranav
you can do with the help of CSS , before print set the CSS of the page . for example:
您可以借助 CSS,在打印之前设置页面的 CSS。例如:
<style type="text/css">
@media print{
body{ background-color:#FFFFFF; background-image:none; color:#000000 }
#ad{ display:none;}
#leftbar{ display:none;}
#contentarea{ width:100%;}
}
</style>
This code when added to the page hides the 2 divs with ids "ad" and "leftbar", plus makes additional changes to the rest of the document when it's printed.
这段代码在添加到页面时隐藏了 ID 为“ad”和“leftbar”的 2 个 div,并在打印时对文档的其余部分进行了额外的更改。
IF you are asking about browser specific settings like print date and time , then i think it is not possible through code. check this out : Remove the default browser header and footer when printing HTML
如果您询问浏览器特定设置,例如打印日期和时间,那么我认为通过代码是不可能的。检查一下: 打印 HTML 时删除默认的浏览器页眉和页脚