我有一个项目需要打印一个包含许多行的html表。我想在每页的顶部显示thead部分。我正在使用IE11浏览器。

<style>
    @media print {
        #Header {
            display: table-header-group;
        }

        table {
            page-break-inside: auto;
        }

        tr {
            page-break-inside: auto;
            position: static;
        }
    }
</style>


<div class="tab-pane active " id="template">
    <table>
        <thead>
            <div id="Header">
                <table style="width: 100%; table-layout: fixed;" id="tbl_1" class="table table-bordered">
                    <tbody>
                        <tr id="1">
                            <td style="height: 18px; border:solid; " ></td>
                            <td style="height: 18px; border:solid; "></td>
                            <td style="height: 18px; border:solid; "></td>
                            <td style="height: 18px; border:solid; "></td>
                        </tr>
                        <tr id="2">
                            <td style="height: 18px; border:solid; "></td>
                            <td style="height: 18px; border:solid; "></td>
                            <td style="height: 18px; border:solid; "></td>
                            <td style="height: 18px; border:solid; "></td>
                        </tr>

                    </tbody>
                </table>
            </div>
        </thead>
        <tbody>
            <div id="contents">
                <!--more then 800 rows in table-->
            </div>
        </tbody>
    </table>
</div>

最佳答案

您可以通过php实现这一点。当我第一次开始使用php时,我的老师让我们用php创建一个“web模板”。此“模板”将确保页面相似,并且具有一致的页眉、导航、页脚等。
您可以创建一个文件(thead.php),其中包含您想要重复的所有代码。我猜你想在<thead></thead>
(thead.php)

<thead>
    <div id="Header">
        <table style="width: 100%; table-layout: fixed;" id="tbl_1" class="table table-bordered">
            <tbody>
                <tr id="1">
                    <td style="height: 18px; border:solid; " ></td>
                    <td style="height: 18px; border:solid; "></td>
                    <td style="height: 18px; border:solid; "></td>
                    <td style="height: 18px; border:solid; "></td>
                </tr>
                <tr id="2">
                    <td style="height: 18px; border:solid; "></td>
                    <td style="height: 18px; border:solid; "></td>
                    <td style="height: 18px; border:solid; "></td>
                    <td style="height: 18px; border:solid; "></td>
                </tr>
            </tbody>
        </table>
    </div>
</thead>

(yourfile.php)
<style>
@media print {
    #Header {
        display: table-header-group;
    }

    table {
        page-break-inside: auto;
    }

    tr {
        page-break-inside: auto;
        position: static;
    }
}
</style>


<div class="tab-pane active " id="template">
    <table>
        <?php include 'thead.php'; ?>
        <tbody>
            <div id="contents">
                <!--more then 800 rows in table-->
            </div>
        </tbody>
    </table>
</div>

这将导致必须将文件类型更改为.php,并且必须使用本地服务器进行测试,因为浏览器无法处理.php文件。我个人使用xampp,但我相信还有很多其他的选择。
如果您还不知道任何php,它实际上是根据通过.php文件中的脚本提供给它的信息为您编写一个html文件。当文件通过服务器运行到浏览器时,它只是简单的html代码,可以处理和显示。因此,最后一个文件,到达浏览器将不会有任何不同,如果你这样做,我已经告诉你,只有很少的代码,你必须写。

09-10 01:31
查看更多