为什么只打印一次?请给我解决方案

M2K*_*M2K 1 html javascript

<html>
<head>
<script type="text/javascript">
    function printpage()
        {document. getElementById ('print').style.display='none';
        window.print()
    }
</script>
</head>
<body>
  <td align="center">
    <input  name="print" type="submit" id="print" value="PRINT" onclick="printpage()" />
  </td>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

单击时,将打开一个打印窗口.一旦我关闭打印窗口并再次打开然后单击打印,而不重新加载,则它将无法工作.

Ahs*_*hid 7

document.getElementById('print').style.display='none';从您的printpage()功能中删除.

在上述情况下,按钮将在另一个单击事件中可见,但是当您打印文档时,该按钮将显示在打印文档上.我对吗?

要防止打印print按钮,您需要使用css媒体查询 @media print

在extrernal样式表中添加以下内容或在HTML页面的<style>标记内<head>添加标记:

 @media print {    
     .noprint { display: none; }
 }?
Run Code Online (Sandbox Code Playgroud)

并添加.noprint

<input  name="print" class="noprint" type="submit" id="print" value="PRINT" onclick="printpage()" />
Run Code Online (Sandbox Code Playgroud)

看看演示

它将打印文档而不打印按钮,您的按钮也将在第二次点击时显示:-)

编辑:

使用HTML如下:

<!DOCTYPE html>
<html>
    <head>

      <meta charset=utf-8 />  
      <title>JS Bin</title>  

      <!-- Your Stylesheet (CSS) -->  
      <style type="text/css">
         @media print {    
           .noprint { display: none; }
         }?
      </style>

      <!-- Your Javascript Function -->  
      <script>
          function printpage() {       
              window.print();
          }
      </script>

    </head>
<body>

<!-- Your Body -->
<p>Only This text will print</p>

<!-- Your Button -->
<input class="noprint" type="button" value="PRINT" onclick="printpage()" />

</body>
</html>
Run Code Online (Sandbox Code Playgroud)

请参阅上述代码