如何在没有URL和日期的情况下打印html,但仍然有顶部的标题?

时间:2017-08-28 11:58:15

标签: javascript html css

这是您可以通过详细信息查看特定主题的时间表的表单:

This is the form where you'll see the schedules of a specific subject with details

单击打印按钮时会发生这种情况:

This is what happen when I click the print button

这是要打印的按钮。

<button onclick="print('to-print')">Print</button>

这就是要打印的内容。

<div id="to-print"> 
<table> 
  <thead>
     <th>Teacher In-charge</th>
     <th>Time</th>
     <th>Section</th>
     <th>Room</th>
   </thead>
   <tbody>
      <tr>
        <td>John Doe</<td>
        <td>M-W-F / 7:00 - 8:00 am</td>
        <td>A</td> 
        <td>Room 1</td>
      </tr>
   </tbody>
</table>
</div>

javascript函数。

<script> 
 function printdiv(printpage) {
   var headstr = "<html><head><title></title></head><body>";
   var footstr = "</body>";
   var newstr = document.all.item(printpage).innerHTML;
   var oldstr = document.body.innerHTML;
   document.body.innerHTML = headstr+newstr+footstr;
   window.print();
   document.body.innerHTML = oldstr;
   return false;
 } 
</script>

我已经在我的css中使用过这种方法但是它也会删除标题

<style> 
 @page { size: auto;  margin: 0mm; }
</style>

1 个答案:

答案 0 :(得分:0)

我使用此代码

@media print {
    @page 
    {
        size: auto;   /* auto is the initial value */
        margin: 0mm;  /* this affects the margin in the printer settings */
    }

    body 
    {
        background-color:#FFFFFF; 
        border: solid 1px black ;
        margin: 0px;  /* this affects the margin on the content before sending to printer */
   }
}

为我工作

希望可以帮助您