简体   繁体   English

选择页面上的最后一个元素(页面媒体)

[英]Selecting the last element on page (paged media)

I want to select the last element on a printed page. 我想选择打印页面上的最后一个元素。 For example, given this HTML (assuming the printed page has a height of 100px): 例如,给定此HTML(假设打印页面的高度为100px):

<!-- other content before -->

<div id="TheThing">
  <section id="a">Some content</section>
  <section id="b">Some content</section>
  <section id="c">This will be the last section on the page</section>
  <section id="d">This section will start on the next page</section>
  <section id="e">This is the last section</section>
</div>

<!-- other content after -->

and this CSS: 和这个CSS:

#TheThing {
  page-break-before: always;
  page-break-after: always;
}

section {
  height: 30px;
  border-bottom: solid 1px black;
  page-break-inside: avoid;
}

I want to be able to select the last element on each printed page , in this case #c (and also #e , although if necessary, this could be easily excluded with :not(:last-child) ) and remove its border. 我希望能够选择每个打印页面上最后一个元素 ,在这种情况下为#c (还有#e ,尽管如有必要,也可以使用:not(:last-child)轻松排除它)并删除其边框。

Approximation of desired output: 所需输出的近似值:

所需的输出

Currently I'm using Prince XML. 目前,我正在使用Prince XML。 I prefer not to do with it JavaScript. 我宁愿不要使用JavaScript。

Is it possible? 可能吗?

Prince's founder and lead programmer has stated in their forum that this is not possible without JavaScript and running the document twice through Prince. Prince的创始人兼首席程序员在其论坛上表示,如果没有JavaScript并通过Prince运行两次文档,这是不可能的。 Sorry :( 对不起:(

https://www.princexml.com/forum/topic/3672/how-to-select-the-last-tr-element-of-a-page-when-it-breaks https://www.princexml.com/forum/topic/3672/how-to-select-the-last-tr-element-of-a-page-when-it-breaks

I don't think this can be controlled. 我认为这是无法控制的。 The main reason is that there is no such thing as "last in page" because depending on the paper you are printing on, more elements could be there and this takes place after the CSS is applied to the HTML elements, and your document has no way of accessing this information. 主要原因是没有“最后一页”之类的东西,因为根据要打印的纸张,可能会存在更多元素,并且这是在将CSS应用于HTML元素之后发生的,而您的文档没有访问此信息的方式。

But, if you know yourself 100% what type of paper you will be using to print it, let's say A4, you can set a height to the parent and child elements and then with a simple :nth-child() pseudo-selector you will be able to control which one to select to apply the style. 但是,如果您100%知道自己将使用哪种类型的纸张进行打印(例如A4),则可以为父元素和子元素设置高度,然后使用简单的:nth-child()伪选择器进行设置将能够控制选择哪种样式来应用样式。 This is because you know the A4's height, so you can predict accurately, supposing none will add any different options to the Print Preview page (ie zooming, etc.). 这是因为您知道A4的高度,因此可以准确地进行预测,假设没有A4的高度会在“打印预览”页面上添加任何其他选项(例如缩放等)。 Also, you could put this CSS inside the @media print , so it is applied only to the printed pages. 另外,您可以将此CSS放在@media print ,因此它仅适用于打印的页面。

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

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