2014-01-17 33 views
1

我想將一個網站的內容封裝在一個div中(我正在編寫一個用戶同意這種情況的插件)。我做了這樣的:使用jQuery打破頁面包裝正文內容

jQuery("body").append(jQuery('<div id="site-content"></div>')); jQuery("body").children(':not(#site-content)').wrap(jQuery('#site-content'));

出於某種原因,它打破了這個網站:

http://coding.smashingmagazine.com/2012/11/05/writing-fast-memory-efficient-javascript/

這是爲什麼,是有周圍的辦法嗎?

編輯

好吧,$.wrap是不太我想要的東西,但即使你只是使用appendTo移動內容仍然無法正常工作。

例子:

jQuery("body").append(jQuery('<div id="site-content"></div>')); jQuery("body").children(':not(#site-content)').appendTo(jQuery('#site-content'))

這是會發生什麼(我的意思打破網站) enter image description here

這裏的代碼,沒有jQuery的原理:

var contentWrapper, contentContainer, ps_header, node, domSkip = 0, disallowedTags = { 
    "SCRIPT": null, 
    "STYLE": null 
}; 

//Create contentWrapper 
(contentWrapper = document.createElement("div")).className = document.body.className + " content-wrapper"; 
(contentContainer = document.createElement("div")).className = "content-container"; 
contentWrapper.appendChild(contentContainer); 

//Populate contentWrapper with the body, while removing it from the body 
while (document.body.children.length > domSkip) { 
    node = document.body.children[domSkip]; 

    if (node.nodeName in disallowedTags) { 

     domSkip++; 
     continue; 
    } 

    document.body.removeChild(node); 
    contentContainer.appendChild(node); 
} 

//Now drop contentWrapper into the DOM 
document.body.appendChild(contentWrapper); 
+0

你是什麼意思與「休息頁」? – Sergio

+0

在那個頁面的控制檯上執行,你會看到 – LordZardeck

+0

這沒有錯誤,它打破了網站 – LordZardeck

回答