我试图创建一些< page>< / page> a4类取决于其中的内容量.如果内容超过< page>< / page>的高度,我想创建一个新的< page>< / page>对于超出上一页的内容.
var element = document.getElementsByClassName('a4')[0];

var editorHeight = element.offsetHeight;

var pages = element.childElementCount * 1123;

var pageNumber = element.getElementsByClassName('a4').length + 1;

if (editorHeight < pages) {

  var newPage = document.createElement('page');
  newPage.setAttribute('size','A4');
  newPage.setAttribute('class','a4');
  newPage.dataset.pages = pageNumber;
  element.appendChild(newPage);


}
body {
  background: rgb(204,204,204);
}

page {
  background: white;
  display: block;
  text-align: justify;
  margin: 20px;
  margin-bottom: 0.5cm;
  Box-shadow: 0 0 0.5cm rgba(0,0.5);
}

page[size="A4"] {
  width: 21cm;
  height: 29.7cm;
  padding: 20px;
}

@media print {
  body,page {
    margin: 0;
    Box-shadow: 0;
  }
}
<page size="A4">
  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. Contrary to popular belief,a Latin professor
  at Hampden-Sydney College in Virginia,looked up one of the more obscure Latin words,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,"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. Contrary to popular belief,making it over
  2000 years old. Richard Mcclintock,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,very popular during the Renaissance.
  The first line of Lorem Ipsum,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,Lorem Ipsum is not simply random text. It has roots in a piece of classical
  Latin literature from 45 BC,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,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,Lorem Ipsum is not simply
  random text. It has roots in a piece of classical Latin literature from 45 BC,from
  a Lorem Ipsum passage,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.
  Contrary to popular belief,accompanied by English versions from the 1914 translation by H. Rackham.
</page>

从上面的代码中,我检查了新的< page class ='a4'>< / page>已创建但我无法获得超出内容并将其放入新创建的页面中.

如何让新页面填充超过上一页的内容?

解决方法

如果您使用 Summer Note或类似类型的html编辑器,我们可以消除在html中包含文本节点的想法,因为这些编辑器通常将所有内容包装在< p>中.标签.例如,如果您编写以下内容
This is First Line
This is Second Line
This is Third Line

编辑器将生成这样的HTML.

<p>This is First Line</p>
<p>This is Second Line</p>
<p>This is Third Line</p>

生成的HTML也可以有表格,列表和可能是图像,

<h2>A Title</h2>
<p>A 2-3 line paragraph</p>
<hr><!-- just to prettify -->
<table>...</table>
<ul><li>An Un-ordered List</li></ul>
<ol><li>An Un-ordered List</li></ol>
<p><img src="#" alt="An Image"></p>

没有任何权衡,我们无法将它们分成页面.因此,如果我们可以通过一些权衡取舍,我们可以完成这项工作.关于这些HTML编辑器的一个好处是它们倾向于生成大部分Block level Elements作为页面的直接子项.因此,通过计算每个Block元素子元素的位置和高度,我们可以检查子元素是否适合使用简单算法的页面,或者如果不是,则将其移动到下一页面.

function arrangePage($pageElement,index) {
  if (!index) index = 1;
  var pageHeight = $pageElement.height();
  var children = $pageElement.children();
  for (var i = 0; i < children.length; i++) {
    var child = children.eq(i);
    if(i == 0 && child.height() > pageHeight) continue; /* Infinite loop fix :: explained in answer*/
    var bottom = child.height() + child[0].offsetTop;
    if (bottom < pageHeight) continue;
    var newPage = $('<page size="A4">').appendTo('body').append(children.splice(i));
    arrangePage(newPage,index + 1);
  }
  $('<span>').addClass('page-number').html('Page ' + index).appendTo($pageElement); /* Inerts Page Index*/
}
$(function() {
  arrangePage($("page"));
});
body {
  background: rgb(204,204);
}

page {
  position: relative; /* This is mandatory */
  background: white;
  display: block;
  text-align: justify;
  margin: 20px;
  margin-bottom: 0.5cm;
  Box-shadow: 0 0 0.5cm rgba(0,0.5);
}

page[size="A4"] {
  width: 21cm;
  height: 29.7cm;
  padding: 20px;
  overflow: hidden;
}

@media print {
  body,page {
    margin: 0;
    Box-shadow: 0;
  }
}
.page-number {
    position: absolute;
    bottom: 8px;
    left: 8px;
    font-size: 12px;
    color: #9e4200de;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<page size="A4"><p><span style="color: inherit; font-family: inherit; font-size: 14px;"></span></p><h2 class="entry-title fusion-post-title" data-fontsize="18" data-lineheight="27" style="color: rgb(51,51,51); margin-top: 0px; margin-bottom: 28px; font-size: 18px; font-family: muSEO-slab-300,Arial,Helvetica,sans-serif; line-height: 27px; padding-bottom: 0px;">Blog Image Post</h2><h2><div class="post-content" style=""><p style="text-align: center; color: rgb(116,116,116); font-family: &quot;PT Sans&quot;,sans-serif; font-size: 13px; margin-bottom: 20px;"><img src="https://www.jqueryscript.net/images/Universal-Placeholder-Text-Lorem-Ipsum-Generator-getlorem.jpg" style="width: 425px;"><span style="color: rgb(107,113,122);"><br></span></p><p style="color: rgb(116,sans-serif; font-size: 13px; margin-bottom: 20px;"><span style="color: rgb(107,122);">Contrary to popular belief,looked up&nbsp;</span><span style="color: rgb(107,122);">one of the more obscure Latin words,discovered the undoubtable source.</span></p><h3 style="color: rgb(116,sans-serif; font-size: 13px; margin-bottom: 20px;"><ul style="Box-sizing: inherit; color: rgb(0,0); font-family: Verdana,sans-serif; font-size: 15px;"><li style="Box-sizing: inherit;">Item</li><li style="Box-sizing: inherit;">Item</li><li style="Box-sizing: inherit;">Item</li><li style="Box-sizing: inherit;">Item</li></ul></h3><h3 style="color: rgb(116,sans-serif; font-size: 13px; margin-bottom: 20px;">Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of&nbsp; "de Finibus Bonorum&nbsp;<span style="color: rgb(107,122);">et Malorum" (The Extremes of Good and Evil) by Cicero,very popular during the Renaissance.</span><br></h3><ol style="Box-sizing: inherit; color: rgb(0,sans-serif; font-size: 15px;"><li style="Box-sizing: inherit;">First item</li><li style="Box-sizing: inherit;">Second item</li><li style="Box-sizing: inherit;">Third item</li><li style="Box-sizing: inherit;">Fourth item</li></ol><p style="color: rgb(116,122);">The first line of Lorem Ipsum,comes from a line in section</span><span style="color: rgb(107,122);">&nbsp; 1.10.32.&nbsp;</span><span style="color: rgb(107,122);">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&nbsp;</span><span style="color: rgb(107,122);">versions from the 1914 translation by H. Rackham.</span></p><table id="example" class="ui celled table dataTable" cellspacing="0" width="100%" role="grid" aria-describedby="example_info" style="Box-sizing: inherit; border-collapse: separate; width: 865px; background: rgb(255,255,255); margin: 0px; border: 1px solid rgba(34,36,38,0.15); Box-shadow: none; border-radius: 0.285714rem; color: rgba(0,0.87); font-size: 14px; font-family: Lato,&quot;Helvetica Neue&quot;,sans-serif;"><thead style="Box-sizing: inherit; Box-shadow: none;"><tr role="row" style="Box-sizing: inherit;"><th class="sorting_asc" tabindex="0" aria-controls="example" rowspan="1" colspan="1" aria-sort="ascending" aria-label="Name: activate to sort column descending" style="Box-sizing: content-Box; margin: 0px; padding: 0.928571em 20px 0.928571em 0.785714em; text-align: inherit; transition: background 0.1s ease,color 0.1s ease; cursor: auto; background: rgb(249,250,251); vertical-align: inherit; border-bottom-width: 1px; border-bottom-color: rgba(34,0.1); border-left: none; position: relative; border-radius: 0.285714rem 0px 0px; width: 137px;">Name</th><th class="sorting" tabindex="0" aria-controls="example" rowspan="1" colspan="1" aria-label="Position: activate to sort column ascending" style="Box-sizing: content-Box; margin: 0px; padding: 0.928571em 20px 0.928571em 0.785714em; text-align: inherit; transition: background 0.1s ease,0.1); border-left: 1px solid rgba(34,0.1); position: relative; width: 224px;">Position</th><th class="sorting" tabindex="0" aria-controls="example" rowspan="1" colspan="1" aria-label="Office: activate to sort column ascending" style="Box-sizing: content-Box; margin: 0px; padding: 0.928571em 20px 0.928571em 0.785714em; text-align: inherit; transition: background 0.1s ease,0.1); position: relative; width: 104px;">Office</th><th class="sorting" tabindex="0" aria-controls="example" rowspan="1" colspan="1" aria-label="Age: activate to sort column ascending" style="Box-sizing: content-Box; margin: 0px; padding: 0.928571em 20px 0.928571em 0.785714em; text-align: inherit; transition: background 0.1s ease,0.1); position: relative; width: 39px;">Age</th><th class="sorting" tabindex="0" aria-controls="example" rowspan="1" colspan="1" aria-label="Start date: activate to sort column ascending" style="Box-sizing: content-Box; margin: 0px; padding: 0.928571em 20px 0.928571em 0.785714em; text-align: inherit; transition: background 0.1s ease,0.1); position: relative; width: 85px;">Start date</th><th class="sorting" tabindex="0" aria-controls="example" rowspan="1" colspan="1" aria-label="Salary: activate to sort column ascending" style="Box-sizing: content-Box; margin: 0px; padding: 0.928571em 20px 0.928571em 0.785714em; text-align: inherit; transition: background 0.1s ease,0.1); position: relative; border-radius: 0px 0.285714rem 0px 0px; width: 83px;">Salary</th></tr></thead><tfoot style="Box-sizing: inherit; Box-shadow: none;"><tr style="Box-sizing: inherit;"><th rowspan="1" colspan="1" style="Box-sizing: content-Box; margin: 0px; padding: 0.785714em; font-weight: 400; text-align: inherit; transition: background 0.1s ease,color 0.1s ease; cursor: auto; border-top-color: rgba(34,0.15); background: rgb(249,251); vertical-align: middle; border-left: none; border-radius: 0px 0px 0px 0.285714rem;">Name</th><th rowspan="1" colspan="1" style="Box-sizing: content-Box; margin: 0px; padding: 0.785714em; font-weight: 400; text-align: inherit; transition: background 0.1s ease,251); vertical-align: middle; border-left: 1px solid rgba(34,0.1);">Position</th><th rowspan="1" colspan="1" style="Box-sizing: content-Box; margin: 0px; padding: 0.785714em; font-weight: 400; text-align: inherit; transition: background 0.1s ease,0.1);">Office</th><th rowspan="1" colspan="1" style="Box-sizing: content-Box; margin: 0px; padding: 0.785714em; font-weight: 400; text-align: inherit; transition: background 0.1s ease,0.1);">Age</th><th rowspan="1" colspan="1" style="Box-sizing: content-Box; margin: 0px; padding: 0.785714em; font-weight: 400; text-align: inherit; transition: background 0.1s ease,0.1);">Start date</th><th rowspan="1" colspan="1" style="Box-sizing: content-Box; margin: 0px; padding: 0.785714em; font-weight: 400; text-align: inherit; transition: background 0.1s ease,0.1); border-radius: 0px 0px 0.285714rem;">Salary</th></tr></tfoot><tbody style="Box-sizing: inherit;"><tr role="row" class="odd" style="Box-sizing: inherit;"><td class="sorting_1" style="Box-sizing: content-Box; margin: 0px; padding: 0.785714em; transition: background 0.1s ease,color 0.1s ease; text-align: inherit; border-top: none; border-left: none;">Airi Satou</td><td style="Box-sizing: content-Box; margin: 0px; padding: 0.785714em; transition: background 0.1s ease,color 0.1s ease; text-align: inherit; border-top: none; border-left: 1px solid rgba(34,0.1);">Accountant</td><td style="Box-sizing: content-Box; margin: 0px; padding: 0.785714em; transition: background 0.1s ease,0.1);">Tokyo</td><td style="Box-sizing: content-Box; margin: 0px; padding: 0.785714em; transition: background 0.1s ease,0.1);">33</td><td style="Box-sizing: content-Box; margin: 0px; padding: 0.785714em; transition: background 0.1s ease,0.1);">2008/11/28</td><td style="Box-sizing: content-Box; margin: 0px; padding: 0.785714em; transition: background 0.1s ease,0.1);">$162,700</td></tr><tr role="row" class="even" style="Box-sizing: inherit;"><td class="sorting_1" style="Box-sizing: content-Box; margin: 0px; padding: 0.785714em; transition: background 0.1s ease,color 0.1s ease; text-align: inherit; border-top-color: rgba(34,0.1); border-left: none;">Angelica Ramos</td><td style="Box-sizing: content-Box; margin: 0px; padding: 0.785714em; transition: background 0.1s ease,0.1);">Chief Executive Officer (CEO)</td><td style="Box-sizing: content-Box; margin: 0px; padding: 0.785714em; transition: background 0.1s ease,0.1);">London</td><td style="Box-sizing: content-Box; margin: 0px; padding: 0.785714em; transition: background 0.1s ease,0.1);">47</td><td style="Box-sizing: content-Box; margin: 0px; padding: 0.785714em; transition: background 0.1s ease,0.1);">2009/10/09</td><td style="Box-sizing: content-Box; margin: 0px; padding: 0.785714em; transition: background 0.1s ease,0.1);">$1,200,000</td></tr></tbody></table><p style="color: rgb(116,sans-serif; font-size: 13px; margin-bottom: 20px;"><br></p><p style="color: rgb(116,sans-serif; font-size: 13px; margin-bottom: 20px;"><br></p></div></h2><p><span style="color: rgb(116,sans-serif;">Nunc tincidunt,elit non cursus euismod,lacus augue ornare metus,egestas imperdiet nulla nisl quis mauris. Suspendisse a pharetra urna. Morbi dui lectus,pharetra nec elementum eget,vulputate ut nisi. Aliquam accumsan,nulla sed feugiat vehicula,lacus justo semper libero,quis porttitor turpis odio sit amet ligula. Duis dapibus fermentum orci,nec malesuada libero vehicula ut.</span></p><p><span style="color: rgb(116,sans-serif;">Integer soDales,urna eget interdum eleifend,nulla nibh laoreet nisl,quis dignissim mauris dolor eget mi. Donec at mauris enim. Duis nisi tellus,adipiscing a convallis quis,tristique vitae risus. Nullam molestie gravida lobortis. Proin ut nibh quis felis auctor ornare. Cras ultricies,nibh at mollis faucibus,justo eros porttitor mi,quis auctor lectus arcu sit amet nunc.</span></p><p><span style="color: rgb(116,quis auctor lectus arcu sit amet nunc.</span></p><div><p><span style="color: rgb(116,quis auctor lectus arcu sit amet nunc.</span></p></div><div><p><span style="color: rgb(116,quis auctor lectus arcu sit amet nunc.</span></p></div><p><br></p></page>

javascript – 如何为超出内容创建新的页面元素?的更多相关文章

  1. 用canvas做一个DVD待机动画的实现代码

    这篇文章主要介绍了用canvas做一个DVD待机动画的实现代码的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

  2. 使用Html5多媒体实现微信语音功能

    这篇文章主要介绍了使用Html5多媒体实现微信语音功能,需要的朋友可以参考下

  3. HTML5 canvas 瀑布流文字效果的示例代码

    这篇文章主要介绍了HTML5 canvas 瀑布流文字效果的示例代码的相关资料,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  4. HTML5自定义视频播放器源码

    这篇文章主要介绍了HTML5自定义视频播放器源码,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下

  5. Html5 滚动穿透的方法

    这篇文章主要介绍了Html5 滚动穿透的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

  6. HTML5自定义mp3播放器源码

    这篇文章主要介绍了HTML5自定义mp3播放器源码,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下

  7. CSS中实现动画效果-附案例

    这篇文章主要介绍了 CSS中实现动画效果并附上案例代码及实现效果,就是CSS动画样式处理,动画声明需要使用@keyframes name,后面的name是人为定义的动画名称,下面我们来看看文章的具体实现内容吧,需要的小伙伴可以参考一下

  8. html5默认气泡修改的代码详解

    这篇文章主要介绍了html5默认气泡修改的代码详解,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

  9. 基于Html5 canvas实现裁剪图片和马赛克功能及又拍云上传图片 功能

    这篇文章主要介绍了基于Html5 canvas实现裁剪图片和马赛克功能及又拍云上传图片 功能,需要的朋友可以参考下

  10. Html5移动端适配IphoneX等机型的方法

    这篇文章主要介绍了Html5移动端适配IphoneX等机型的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

随机推荐

  1. js中‘!.’是什么意思

  2. Vue如何指定不编译的文件夹和favicon.ico

    这篇文章主要介绍了Vue如何指定不编译的文件夹和favicon.ico,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

  3. 基于JavaScript编写一个图片转PDF转换器

    本文为大家介绍了一个简单的 JavaScript 项目,可以将图片转换为 PDF 文件。你可以从本地选择任何一张图片,只需点击一下即可将其转换为 PDF 文件,感兴趣的可以动手尝试一下

  4. jquery点赞功能实现代码 点个赞吧!

    点赞功能很多地方都会出现,如何实现爱心点赞功能,这篇文章主要为大家详细介绍了jquery点赞功能实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  5. AngularJs上传前预览图片的实例代码

    使用AngularJs进行开发,在项目中,经常会遇到上传图片后,需在一旁预览图片内容,怎么实现这样的功能呢?今天小编给大家分享AugularJs上传前预览图片的实现代码,需要的朋友参考下吧

  6. JavaScript面向对象编程入门教程

    这篇文章主要介绍了JavaScript面向对象编程的相关概念,例如类、对象、属性、方法等面向对象的术语,并以实例讲解各种术语的使用,非常好的一篇面向对象入门教程,其它语言也可以参考哦

  7. jQuery中的通配符选择器使用总结

    通配符在控制input标签时相当好用,这里简单进行了jQuery中的通配符选择器使用总结,需要的朋友可以参考下

  8. javascript 动态调整图片尺寸实现代码

    在自己的网站上更新文章时一个比较常见的问题是:文章插图太宽,使整个网页都变形了。如果对每个插图都先进行缩放再插入的话,太麻烦了。

  9. jquery ajaxfileupload异步上传插件

    这篇文章主要为大家详细介绍了jquery ajaxfileupload异步上传插件,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  10. React学习之受控组件与数据共享实例分析

    这篇文章主要介绍了React学习之受控组件与数据共享,结合实例形式分析了React受控组件与组件间数据共享相关原理与使用技巧,需要的朋友可以参考下

返回
顶部