打印网页时如何隐藏元素?

发布于 2025-01-10 12:31:37 字数 280 浏览 0 评论 0 原文

我的网页上有一个用于打印网页的链接。但是,该链接在打印输出本身中也可见。

当我单击打印链接时,是否有 javascript 或 HTML 代码会隐藏链接按钮?

示例:

 "Good Evening"
 Print (click Here To Print)

我想在打印文本“Good Evening”时隐藏此“Print”标签。 “打印”标签不应出现在打印输出本身上。

I have a link on my webpage to print the webpage. However, the link is also visible in the printout itself.

Is there javascript or HTML code which would hide the link button when I click the print link?

Example:

 "Good Evening"
 Print (click Here To Print)

I want to hide this "Print" label when it prints the text "Good Evening". The "Print" label should not show on the printout itself.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

想你的星星会说话 2025-01-17 12:31:37

在样式表中添加:

@media print
{    
    .no-print, .no-print *
    {
        display: none !important;
    }
}

然后在您不希望出现在打印版本中的 HTML 中添加 class='no-print' (或将 no-print 类添加到现有的类语句中) ,例如您的按钮。

In your stylesheet add:

@media print
{    
    .no-print, .no-print *
    {
        display: none !important;
    }
}

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.

话少心凉 2025-01-17 12:31:37

Bootstrap 3 有它的自己的 称为:

hidden-print

它的定义如下:

@media print {
  .hidden-print {
    display: none !important;
  }
}

您不必自己定义它。


Bootstrap 4Bootstrap5 这已更改为:

.d-print-none

Bootstrap 3 has its own class for this called:

hidden-print

It is defined like this:

@media print {
  .hidden-print {
    display: none !important;
  }
}

You do not have to define it on your own.


In Bootstrap 4 and Bootstrap5 this has changed to:

.d-print-none
情独悲 2025-01-17 12:31:37

最佳实践是使用专门用于打印的样式表,并将其media属性设置为print

在其中显示/隐藏要打印在纸张上的元素。

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

The best practice is to use a style sheet specifically for printing, and set its media attribute to print.

In it, show/hide the elements that you want to be printed on paper.

<link rel="stylesheet" type="text/css" href="print.css" media="print" />
So要识趣 2025-01-17 12:31:37

这是一个简单的解决方案
放置此 CSS

@media print{
   .noprint{
       display:none;
   }
}

,这里是 HTML

<div class="noprint">
    element that need to be hidden when printing
</div>

Here is a simple solution
put this CSS

@media print{
   .noprint{
       display:none;
   }
}

and here is the HTML

<div class="noprint">
    element that need to be hidden when printing
</div>
栀子花开つ 2025-01-17 12:31:37

CSS 文件

@media print
{
    #pager,
    form,
    .no-print
    {
        display: none !important;
        height: 0;
    }


    .no-print, .no-print *{
        display: none !important;
        height: 0;
    }
}

HTML 标头

<link href="/theme/css/ui/ui.print.css?version=x.x.x" media="print" rel="stylesheet" type="text/css" >

元素

<div class="no-print"></div>

CSS FILE

@media print
{
    #pager,
    form,
    .no-print
    {
        display: none !important;
        height: 0;
    }


    .no-print, .no-print *{
        display: none !important;
        height: 0;
    }
}

HTML HEADER

<link href="/theme/css/ui/ui.print.css?version=x.x.x" media="print" rel="stylesheet" type="text/css" >

ELEMENT

<div class="no-print"></div>
独木成林 2025-01-17 12:31:37

您可以将链接放置在 div 中,然后在锚标记上使用 JavaScript 在单击时隐藏该 div。示例(未经测试,可能需要调整,但您明白了):

<div id="printOption">
    <a href="javascript:void();" 
       onclick="document.getElementById('printOption').style.visibility = 'hidden'; 
       document.print(); 
       return true;">
       Print
    </a>
</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):

<div id="printOption">
    <a href="javascript:void();" 
       onclick="document.getElementById('printOption').style.visibility = 'hidden'; 
       document.print(); 
       return true;">
       Print
    </a>
</div>

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.

删除会话 2025-01-17 12:31:37
@media print {
  .no-print {
    visibility: hidden;
  }
}
<div class="no-print">
  Nope
</div>

<div>
  Yup
</div>

@media print {
  .no-print {
    visibility: hidden;
  }
}
<div class="no-print">
  Nope
</div>

<div>
  Yup
</div>

软甜啾 2025-01-17 12:31:37

最好的办法是创建页面的“仅限打印”版本。

哦,等等……这已经不是 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".

夜访吸血鬼 2025-01-17 12:31:37

diodus 接受的答案对我们中的一些人(如果不是所有人)来说不起作用。
我仍然无法隐藏“打印此”按钮,以免出现在纸上。

Clint Pachl 通过添加来调用 css 文件的小调整

      media="screen, print" 

,而不仅仅是

      media="screen"

解决了这个问题。因此,为了清楚起见,也因为很难看到 Clint Pachl 在评论中隐藏了额外的帮助。
用户应在 css 文件中包含具有所需格式的“,print”。

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

而不仅仅是默认的 media =“screen”。

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

我认为这可以为每个人解决这个问题。

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

      media="screen, print" 

and not just

      media="screen"

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.

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

and not the default media = "screen" only.

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

That i think solves this problem for everyone.

无人接听 2025-01-17 12:31:37

如果您的 Javascript 会干扰各个元素的样式属性,从而覆盖 !important,我建议处理 onbeforeprintonafterprint 事件。 https://developer.mozilla.org/en-US/docs /Web/API/WindowEventHandlers/onbeforeprint

If you have Javascript that interferes with the style property of individual elements, thus overriding !important, I suggest handling the events onbeforeprint and onafterprint. https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onbeforeprint

几度春秋 2025-01-17 12:31:37

正如 Elias Hasle 所说,JavaScript 可以覆盖 !important。因此,我通过理论实现扩展了他的答案。

此代码标识所有带有 no-print 类的元素,在打印之前用 CSS 隐藏它们,并在打印后恢复原始样式:

var noPrintElements = [];

window.addEventListener("beforeprint", function(event) {
   var hideMe = document.getElementsByClassName("no-print");
   noPrintElements = [];
   Array.prototype.forEach.call(hideMe, function(item, index) {
      noPrintElements.push({"element": item, "display": item.style.display });
      item.style.display = 'none'; // hide the element
   });   
});

window.addEventListener("afterprint", function(event) {
   Array.prototype.forEach.call(noPrintElements, function(item, index) {
      item.element.style.display = item.display; // restore the element
   });      
   noPrintElements = []; // just to be on the safe side
});

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:

var noPrintElements = [];

window.addEventListener("beforeprint", function(event) {
   var hideMe = document.getElementsByClassName("no-print");
   noPrintElements = [];
   Array.prototype.forEach.call(hideMe, function(item, index) {
      noPrintElements.push({"element": item, "display": item.style.display });
      item.style.display = 'none'; // hide the element
   });   
});

window.addEventListener("afterprint", function(event) {
   Array.prototype.forEach.call(noPrintElements, function(item, index) {
      item.element.style.display = item.display; // restore the element
   });      
   noPrintElements = []; // just to be on the safe side
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文