我想获取一个 html 文档(一本书的章节)并将其分成页面(一个 DIV 数组,每个包含一个适合 DIV 规定尺寸的 html 内容页面)。我使用以下代码(在此站点上找到!)遍历 DOM。
function walk(node, func)
{
func(node);
node = node.firstChild;
while (node)
{
walk(node, func);
node = node.nextSibling;
}
};
func 函数称为 test,如下所示。
function test(node)
{
var copy=node.cloneNode(false);
currentPageInArray.appendChild(copy);
//Test if we still fit
if( $(currentPageInArray).height() <= maxPageHeight )
{
//All good
}
else
{
//We dont fit anymore
//Remove node that made us exceed the height
currentPageInArray.removeChild(copy);
createNewPage();
currentPageInArray.appendChild(copy); //into new page
}
}
我的页面生成正确,但是,我丢失了所有样式,如斜体、粗体、标题等。如果我尝试 clone(true),许多元素会重复多次。我怎样才能解决这个问题?提前致谢。