如何在CSS中打印除最后一页之外的每一页的页脚文本?

Hou*_*run 5 html css

使用下面的代码,该类.note是一个页脚文本,我希望将其打印在除最后一页之外的每一页上。

@media print {
  body {
    zoom: 85%;
  }
  p.note {
    bottom: -20px;
    position: fixed;
    margin-top: 10px;
  }
}

.page-break {
  display: block;
  page-break-before: always;
}
Run Code Online (Sandbox Code Playgroud)
<div class="wrappeer">
  <p>
    <h3>What is Lorem Ipsum?</h3>
    Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has
    survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing
    software like Aldus PageMaker including versions of Lorem Ipsum.
  </p>
  <div class="page-break"></div>
  <p>
    <h3>Why do we use it?</h3>
    It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here,
    content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various
    versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
  </p>
  <div class="page-break"></div>
  <p>
    <h3>Where does it come from?</h3>
    Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up
    one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum
    et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section
    1.10.32. The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by
    English versions from the 1914 translation by H. Rackham.
  </p>
  <div class="page-break"></div>
  <p>
    <h3>Where can I get some?</h3>
    There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum,
    you need to be sure there isn't anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary
    of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc.
  </p>

  <p class="note">In the event that you have doubts or irregularities or both, acts of employees and services Please contact the company by dialing <span style="font-size:14px;font-weight: bold;">089 00 89 00</span> or <span style="font-size:14px;font-weight: bold;">08900 89 00.</span></p>

</div>
Run Code Online (Sandbox Code Playgroud)

然而,结果却在每一页上打印页脚文本。我尝试过一些CSS3,@page例如@page:last{},@page:last-child{}p.note:not(:last){}来定位最后一页,但没有任何帮助。在不使用 JS 的情况下,我更喜欢任何 CSS 样式来解决这个问题,谢谢。

A. *_*shu 2

阅读文档后,似乎仅使用 css 是不可能的(截至 2018 年末)。看来@media print 无法捕获最后一个打印页。

我会这样解决这个问题:

  1. 将 .note 位置更改为绝对位置并将其从代码中删除。
  2. 更改 html,将 .note p 放置在每个 .page-break div 之前。

像这样的东西:

<script>
  var pageBreak = document.getElementsByClassName('page-break');
  var note = '<p class="note">In the event that you have doubts or irregularities or both, acts of employees and services Please contact the company by dialing <span style="font-size:14px;font-weight: bold;">089 00 89 00</span> or <span style="font-size:14px;font-weight: bold;">08900 89 00.</span></p>';
  for (var i=0; i < pageBreak.length; i++) {
    pageBreak[i].insertAdjacentHTML('beforebegin', note);
  }
</script>
Run Code Online (Sandbox Code Playgroud)

我想这是唯一应该链接以供进一步阅读的内容:insertAdjacentHTML

希望它能解决它。

  • 您应该将这些 h3 从您的 p 中取出,因为它不是有效的 html,但就您的问题而言,它不相关。

旧的不工作建议:用 css 隐藏最后一个 .wrapper .note 怎么样:

.wrapper:last-child .note { display:none; }
Run Code Online (Sandbox Code Playgroud)

编辑:

.page-break:last-child + p + .note { display: none}
Run Code Online (Sandbox Code Playgroud)