How to embed all <body> in the created new div with jQuery?
I want to wrap all the contents of Body in <div>, not including the tag <body>, details,
change DOM files from
<html>
<body>i'm a body</body>
<p>i'm out of body</p>
</html>
to (just put everything inside the body in one div)
<html>
<body>
<div id='bodyContainer'>
i'm a body
</div>
<div id='footer'>
i'm a footer
</div>
</body>
<p>i'm out of body</p>
</html>
I tried to do this using jQuery
$(document).ready(function() {
$("body").append("<div id='container'>I'm a body-container</div>");
$("body").append("<div id='footer'>i'm testing!</div>");
});
but failed to change the DOM as
<html>
<div id='bodyContainer'>
<body>
i'm a body
</body>
<p>i'm out of body</p>
</div>
<div id='footer'>
i'm a footer
</div>
</html>
this is not what i want, see example http://jsfiddle.net/7szM4/2/ Thanks.
+3
3 answers
$(function() {
$('body').wrapInner('<div id="bodyContainer"/>');
$('<div />',{id:"footer",text :"i'm a footer"})
.insertAfter('#bodyContainer');
});
This should complete the task. Here is the demo : http://jsfiddle.net/DeNjE/
+1
user372551
source
share