更改字体系列和字体大小以进行打印

时间:2019-05-24 19:00:45

标签: javascript

这是我的Javascript

<script type="text/javascript">
            function printInfo(ele) {
              var openWindow = window.open();
              openWindow.document.write(ele.previousSibling.innerHTML,'');
              openWindow.document.body.setAttribute('style', 'font-family:Arial !important;');
              openWindow.document.body.setAttribute('style', 'font-size:36px;');
              openWindow.document.close();
              openWindow.focus();
              openWindow.print();
              openWindow.close();
            }
          </script>

它将更改文档中的字体系列,但表中的文本除外。请帮忙。我使用以下方式打印:

打开div-文档-关闭div-a onclick printInfo(this)

<table border="0" style="margin-left: 10px;">
  <tr style="margin: 0px;">
     <td>Nama</td>
     <td style="padding:0 10px 0 10px">:</td>
     <td><?php echo "$nmlengkap";?></td>
  </tr>
</table>

1 个答案:

答案 0 :(得分:1)

为表格提供一个ID,以便您也可以在表格上设置样式

<script type="text/javascript">
            function printInfo(ele) {
              var openWindow = window.open();
              openWindow.document.write(ele.previousSibling.innerHTML,'');
              tbl = openWindow.document.getElementById("tbl");
              tbl.setAttribute('style', 'font-family:Arial !important;');
              tbl.setAttribute('style', 'font-family:Arial 'font-size:36px;');
              openWindow.document.body.setAttribute('style', 'font-family:Arial !important;');
              openWindow.document.body.setAttribute('style', 'font-size:36px;');
              openWindow.document.close();
              openWindow.focus();
              openWindow.print();
              openWindow.close();
            }
          </script>

和桌子:

<table id='tbl' border="0" style="margin-left: 10px;">
  <tr style="margin: 0px;">
     <td>Nama</td>
     <td style="padding:0 10px 0 10px">:</td>
     <td><?php echo "$nmlengkap";?></td>
  </tr>
</table>