2011-07-30 33 views
0

我要帶一個html文件(書章),並將其分成頁(DIV,一個陣列時保留風格各包含HTML內容的網頁,將適合的規定尺寸內DIV)。我用下面的代碼(在這個網站上找到!)走DOM。如何克隆一個節點

function walk(node, func) 
{ 
    func(node); 
    node = node.firstChild; 
    while (node) 
    { 
     walk(node, func); 
     node = node.nextSibling; 
    } 
}; 

func函數被稱爲測試,如下所示。

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

我的網頁被正確地產生,但是,我失去所有的風格,如斜體,粗體,標題等,如果我嘗試克隆(真),許多元素得到重複多次。我怎樣才能解決這個問題?提前致謝。

+1

是否有一個原因,你不是簡單地在你的樣式表中設計你的內容? – user113716

回答

1

可在檢索使用currentStyle(IE < 9)getComputedStyle(其他)的每一個元素或的當前佈局,並將其應用到克隆的元素。