打印网页时如何隐藏元素?
我的网页上有一个用于打印网页的链接。但是,该链接在打印输出本身中也可见。
当我单击打印链接时,是否有 javascript 或 HTML 代码会隐藏链接按钮?
示例:
"Good Evening"
Print (click Here To Print)
我想在打印文本“Good Evening”时隐藏此“Print”标签。 “打印”标签不应出现在打印输出本身上。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
在样式表中添加:
然后在您不希望出现在打印版本中的 HTML 中添加
class='no-print'
(或将 no-print 类添加到现有的类语句中) ,例如您的按钮。In your stylesheet add:
Then add
class='no-print'
(or add the no-print class to an existing class statement) in your HTML that you don't want to appear in the printed version, such as your button.Bootstrap 3 有它的自己的类 称为:
它的定义如下:
您不必自己定义它。
在 Bootstrap 4 和 Bootstrap5 这已更改为:
Bootstrap 3 has its own class for this called:
It is defined like this:
You do not have to define it on your own.
In Bootstrap 4 and Bootstrap5 this has changed to:
最佳实践是使用专门用于打印的样式表,并将其
media
属性设置为print
。在其中显示/隐藏要打印在纸张上的元素。
The best practice is to use a style sheet specifically for printing, and set its
media
attribute toprint
.In it, show/hide the elements that you want to be printed on paper.
这是一个简单的解决方案
放置此 CSS
,这里是 HTML
Here is a simple solution
put this CSS
and here is the HTML
CSS 文件
HTML 标头
元素
CSS FILE
HTML HEADER
ELEMENT
您可以将链接放置在 div 中,然后在锚标记上使用 JavaScript 在单击时隐藏该 div。示例(未经测试,可能需要调整,但您明白了):
缺点是一旦单击,按钮就会消失,并且页面上会丢失该选项(尽管总是有 Ctrl+P)。
更好的解决方案是创建一个打印样式表,并在该样式表中指定 printOption ID(或任何您所称的名称)的隐藏状态。您可以在 HTML 的 head 部分执行此操作,并指定带有媒体属性的第二个样式表。
You could place the link within a div, then use JavaScript on the anchor tag to hide the div when clicked. Example (not tested, may need to be tweaked but you get the idea):
The downside is that once clicked, the button disappears and they lose that option on the page (there's always Ctrl+P though).
The better solution would be to create a print stylesheet and within that stylesheet specify the hidden status of the
printOption
ID (or whatever you call it). You can do this in the head section of the HTML and specify a second stylesheet with a media attribute.最好的办法是创建页面的“仅限打印”版本。
哦,等等……这已经不是 1999 年了。使用带有“display: none”的打印 CSS。
The best thing to do is to create a "print-only" version of the page.
Oh, wait... this isn't 1999 anymore. Use a print CSS with "display: none".
diodus 接受的答案对我们中的一些人(如果不是所有人)来说不起作用。
我仍然无法隐藏“打印此”按钮,以免出现在纸上。
Clint Pachl 通过添加来调用 css 文件的小调整
,而不仅仅是
解决了这个问题。因此,为了清楚起见,也因为很难看到 Clint Pachl 在评论中隐藏了额外的帮助。
用户应在 css 文件中包含具有所需格式的“,print”。
而不仅仅是默认的 media =“screen”。
我认为这可以为每个人解决这个问题。
The accepted answer by diodus is not working for some if not all of us.
I could not still hide my Print this button from going out on to paper.
The little adjustment by Clint Pachl of calling css file by adding on
and not just
is solving this problem. So for clarity and because it is not easy to see Clint Pachl hidden additional help in comments.
The user should include the ",print" in css file with the desired formating.
and not the default media = "screen" only.
That i think solves this problem for everyone.
如果您的 Javascript 会干扰各个元素的样式属性,从而覆盖
!important
,我建议处理onbeforeprint
和onafterprint
事件。 https://developer.mozilla.org/en-US/docs /Web/API/WindowEventHandlers/onbeforeprintIf you have Javascript that interferes with the style property of individual elements, thus overriding
!important
, I suggest handling the eventsonbeforeprint
andonafterprint
. https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onbeforeprint正如 Elias Hasle 所说,JavaScript 可以覆盖
!important
。因此,我通过理论实现扩展了他的答案。此代码标识所有带有
no-print
类的元素,在打印之前用 CSS 隐藏它们,并在打印后恢复原始样式:As Elias Hasle said, JavaScript can override
!important
. So, I extended his answer with a theoretical implementation.This code identifies all elements with the class
no-print
, hides them with CSS before printing, and restores the original style after printing: