html - 打印时隐藏按钮

标签 html css

我有一个页面,底部有 3 个按钮,代码如下:

    function printpage() {
        //Get the print button and put it into a variable
        var printButton = document.getElementById("printpagebutton");
        //Set the print button visibility to 'hidden' 
        printButton.style.visibility = 'hidden';
        //Print the page content
        window.print()
        printButton.style.visibility = 'visible';
    }
#options {
	align-content:center;
	align-items:center;
    text-align: center;
}
<div id="options">
<input type="submit" value="post news" >
<input id="printpagebutton" type="button" value="print news" onclick="printpage()"/>
<input type="button" value="re-enter the news">
</div>

我设法在打印时隐藏了打印按钮,但我不能隐藏其他按钮。

我在互联网上搜索了解决方案,大多数问题都是通过在 css 中添加 display:none; 来回答的,但我最终在屏幕上看到了 3 个隐藏按钮。 我只想在打印时隐藏按钮

答案可能很简单,我在网络开发方面的知识很丰富。

提前谢谢你。

最佳答案

您可以使用 CSS @media 查询。例如:

@media print {
  #printPageButton {
    display: none;
  }
}
<button id="printPageButton" onClick="window.print();">Print</button>

@media print block 中定义的样式仅在打印页面时应用。您可以通过单击代码段中的打印按钮来测试它;你会得到一个空白页。

关于html - 打印时隐藏按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31425557/

相关文章:

javascript - 增加 UP 大小的动态 div 高度

javascript - 为所有最新浏览器和所有文档类型(xhtml、html、html5)重置默认颜色 css 设置的所有已访问站点颜色

在函数中返回未定义的 Javascript 原型(prototype)属性

css - 这是怎么做到的?向下滚动 = 流畅的幻灯片(在 Google Cardboard 网站上)

html - 从不显示到显示 block 的 CSS 转换,反之亦然

android - Phonegap 固定宽度问题

div 内的 CSS HTML 文本区域最大高度减去标题

javascript - <li> 元素的目标子元素

css - 倾斜图像的一个 Angular

css - 什么被誉为目前最好的 IE PNG 修复程序?