在yii2中打印页面内容

时间:2016-05-21 05:26:36

标签: javascript html yii2-advanced-app

我正面临打印页面内容的问题。我使用过window.print(),但它会打印整个窗口。我只需要在我的页面中打印一些内容。请给我一个解决方案。

代码:

<html>
  <head>
    <script>
      function printContent(el)
      {
         var restorepage = document.body.innerHTML;
         var printcontent = document.getElementById(el).innerHTML;
         document.body.innerHTML = printcontent;
         window.print();
         document.body.innerHTML = restorepage;
     }
   </script>
  </head> 
  <body> 
    <h1>My page</h1>
    <div id="div1">DIV 1 content...</div>
    <button onclick="printContent('div1')">Print Content</button>
  </body>
</html>

1 个答案:

答案 0 :(得分:0)

当我在内容div中使用我的打印功能时,我可以打印内容页面。

相关问题