簡體   English   中英

從 html 頁打印表格

[英]Print a table from an html page

我有一個 html 頁面,需要從中將表格發送到打印機。 我現在正在使用 window.print .. 但它會打印整頁...而我只需要打印表格。 有任何想法嗎?

  1. 您可以使用媒體類型打印這里是提示,如何使用樣式表打印 html 頁面)。

  2. 您可以通過彈出窗口 window 實現 - 在此 window 中僅顯示表格並將其發送到打印機。

簡單例子

<script>
    function printDiv() {
        var divToPrint = document.getElementById('areaToPrint');
        newWin = window.open("");
        newWin.document.write(divToPrint.outerHTML);
        newWin.print();
        newWin.close();
   }
</script>

您可以為頁面的所有不需要的部分提供樣式display:none 這樣你就可以只打印表格。

例如:

<style>
    @media only print {
        footer, header, .sidebar {
            display:none;
        }
    }
</style>

為了讓它工作,我還需要將這一行放在文檔的頭部部分之間。

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM