代码之家  ›  专栏  ›  技术社区  ›  Houy Narun

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

  •  3
  • Houy Narun  · 技术社区  · 6 年前

    使用下面的代码,类 .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>

    然而,结果却是在页面上打印页脚文本。我试过一些CSS3 @page @page:last{} , @page:last-child{} p.note:not(:last){} 以最后一页为目标,但没有任何帮助。不用JS,我更喜欢任何CSS样式来解决这个问题,谢谢。

    2 回复  |  直到 6 年前
        1
  •  1
  •   A. Meshu    6 年前

    在阅读文档后,似乎仅使用CSS是不可能的(至于2018年底)。@media print似乎无法捕获最后一个打印页。

    我会这样解决:

    1. 更改。将位置记为绝对位置并将其从代码中删除。
    2. 更改.note p将放在every.page break div之前的HTML。

    就像这样:

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

    我想这是唯一一个需要进一步阅读的链接: insertAdjacentHTML

    希望能解决。

    • 你应该把那些h3从你的p中去掉,因为它不是有效的html,但是对于你的问题,它是无关的。

    旧的不工作建议: 如何隐藏最后一个.wrapper.note与css类似:

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

    编辑:

    .page-break:last-child + p + .note { display: none}
    
        2
  •  0
  •   jawahar N    6 年前

    希望,你在所有页面上都使用了相同的内容。但是,只需要删除一页的页脚文本。因此,我们可以使用通用的JavaScript代码在特定页面中隐藏页脚文本。请参见下面的代码,

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

    首先,我们需要使用JS获取当前文件名。然后检查文件名,如果条件为真,则页脚文本将隐藏。

    例如,您有3页意味着前两页将显示页脚文本。但是,对于mytest.cfm(last)页面,我们不需要页脚文本。所以,使用上面的代码,我们可以隐藏页脚文本。

    希望,这会有所帮助。