只需添加</div>
然后?
$text = '<div class="individualPage">'
. wordwrap($text, 5, '</div><div class="individualPage">')
. '</div>';
但是,您可以使用 javascript 做得更好:您可以根据查看者的屏幕大小进行分页。
只需将您的 HTML 设置为:
<div id="target">...</div>
为页面添加一些 CSS:
#target {
white-space: pre-wrap; /* respect line breaks */
}
.individualPage {
border: 1px solid black;
padding: 5px;
}
然后使用以下代码:
var contentBox = $('#target');
//get the text as an array of word-like things
var words = contentBox.text().split(' ');
function paginate() {
//create a div to build the pages in
var newPage = $('<div class="individualPage" />');
contentBox.empty().append(newPage);
//start off with no page text
var pageText = null;
for(var i = 0; i < words.length; i++) {
//add the next word to the pageText
var betterPageText = pageText ? pageText + ' ' + words[i]
: words[i];
newPage.text(betterPageText);
//Check if the page is too long
if(newPage.height() > $(window).height()) {
//revert the text
newPage.text(pageText);
//and insert a copy of the page at the start of the document
newPage.clone().insertBefore(newPage);
//start a new page
pageText = null;
} else {
//this longer text still fits
pageText = betterPageText;
}
}
}
$(window).resize(paginate).resize();
这将与 PHP 解决方案一起使用,如果禁用 javascript,则提供向后兼容性。