问题
here i have some of the tables , i need to print this html page, with page number,the table content may differ dynamically, i unable to print the page number dynamically ,I have tried so many ways but i unable to find solution yet. I need to Print page Number like "Page no 2/6"
Below is my html sample
<style>
.page {
page-break-before:always;
}
@page {
size: A4;
margin: 0;
}
@media print {
@page {
size: A4;
margin: 0;
}
}
</style>
<table class='page'>
<tr><td></td></tr>....
</table>
<table class='page'>
<tr><td></td></tr>....
</table>
<table class='page'>
<tr><td></td></tr>....
</table>
<table class='page'>
<tr><td></td></tr>....
</table >
<script>
window.print();
</script>
回答1:
If you want you can use counter
body{
counter-reset: page;
}
.page-counter:after {
counter-increment: page;
content: "Page " counter(page);
}
<div class="page">
fake content
<div class="page-counter"></div>
</div>
<div class="page">
fake content
<div class="page-counter"></div>
</div>
If you want to use " page 1 / 10" you can create a div with spans inside of it equal to the number pages you have
body{
counter-reset: page;
}
#total-counter{
counter-reset: totalPages;
}
#total-counter span{
counter-increment: totalPages;
}
.page-counter:after {
counter-increment: page;
content: "Page " counter(page) " / " counter(totalPages);
}
<div id="total-counter">
<span></span>
<span></span>
</div>
<div class="page">
fake content
<div class="page-counter"></div>
</div>
<div class="page">
fake content
<div class="page-counter"></div>
</div>
来源:https://stackoverflow.com/questions/63369672/how-to-print-page-number-in-print-preview-in-html-page-contains-table