简体   繁体   English

如何在CSS的最后一页以外的每个页面上打印页脚文本?

[英]How to print footer text on every page except the last page in CSS?

With this below code, the class .note is a footer text that I would like it to be printed on very page except the last page. 使用下面的代码,类.note是一个页脚文本,我希望它打印在除最后一页之外的页面上。

 @media print { body { zoom: 85%; } p.note { bottom: -20px; position: fixed; margin-top: 10px; } } .page-break { display: block; page-break-before: always; } 
 <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> 

Yet, it turned out to print footer text on very page. 然而,结果是在页面上打印页脚文本。 I have tried some CSS3 of @page such as @page:last{} , @page:last-child{} and p.note:not(:last){} to target the last page, but none helps. 我已经尝试了一些@page CSS3,例如@page:last{}@page:last-child{}p.note:not(:last){}来定位最后一页,但没有人帮忙。 Without using JS, I prefer any CSS style solve this problem, thanks. 不使用JS,我更喜欢任何CSS样式解决这个问题,谢谢。

After reading the docs it seems that it's impossible with css only (as for late 2018). 在阅读文档后,似乎只有css是不可能的(至2018年末)。 It seems that @media print cannot catch the last printing page. 似乎@media print无法捕获最后一个打印页面。

I would solve that like that: 我会这样解决:

  1. Change .note position to absolute and delete it from your code. 将.note位置更改为绝对位置并将其从代码中删除。
  2. Change html that the .note p will be placed before every .page-break div. 更改.tml p将放在每个.page-break div之前的html。

something like that: 类似的东西:

<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>

I guess this is the only thing that should be linked for further reading: insertAdjacentHTML 我想这是唯一应该进一步阅读的内容: insertAdjacentHTML

Hope it solve it. 希望它能解决它。

  • you should put those h3 out from your p cause it's not valid html, but as for your question it's not related. 你应该把那些h3从你的p中拿出来,因为它不是有效的html,但至于你的问题则没有关系。

Old NOT WORKING suggests: How about hide the last .wrapper .note with css like: 老不工作建议:如何用css隐藏最后的.wrapper .note:

.wrapper:last-child .note { display:none; }

Edit: 编辑:

.page-break:last-child + p + .note { display: none}

Hope, you have used same content in all pages. 希望您在所有页面中使用了相同的内容。 But, need to remove the footer text for one page only. 但是,只需删除一页的页脚文本。 So, we can able to hide that footer text in particular page using common javascript code. 因此,我们可以使用常见的JavaScript代码隐藏特定页面中的页脚文本。 Please see below code, 请看下面的代码,

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript">
    var getUrl = window.location.pathname.split('/');
    if( getUrl == 'myTest.cfm') {
        $(".note").css("display", "none");
    }
</script>

First we need to get the current file name using JS. 首先,我们需要使用JS获取当前文件名。 And then check the filename if condition true means, footer text will be hide. 如果条件为true则检查文件名,页脚文本将被隐藏。

For example, you have 3 pages means, in the first two pages the footer text will be displayed. 例如,您有3页表示,在前两页中将显示页脚文本。 But, we don't need footer text for myTest.cfm (last) page. 但是,我们不需要myTest.cfm(最后)页面的页脚文本。 So, using above code, we can able to hide the footer text. 因此,使用上面的代码,我们可以隐藏页脚文本。

Hope, this will help. 希望,这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM