tags:

views:

63

answers:

3

I want to wrap all Body content into a <div>, not including the <body> tag, details,

change files DOM from

<html>
    <body>i'm a body</body>
    <p>i'm out of body</p>
</html>

to (just put all inside of body into 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 make it happen by 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 reform 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, please see example http://jsfiddle.net/7szM4/2/ Thanks.

A: 

try this:

$(document).ready(function() {
    var b = $("body");
    b.text("");
    b.append("<div id='container'>I'm a body-container</div>");
    b.append("<div id='footer'>i'm testing!</div>");
});​

PS:- there was an error in your script on jsFiddle. Put ); at end.

Here is jsFiddle page

TheVillageIdiot
thanks, i updated the code, but not 'update' the link :) it's not a root cause to make it fail...any way, thanks
Elaine
I think 'wrap' is a good point, but the 'border' shows it wraps all the <html> into the <div>, that's why I created a <p> outside for a sample.. but this is not what I want..
Elaine
A: 
$(document).ready(function() { 
    $("body").html("<div id='container'>I'm a body container</div><div id='footer'>i'm testing</div>");
});

Basically use the html() function to replace all the HTML within the body.

JasCav
-1 not a good implementation
Ninja Dude
surely I don't want to do like this, it's of bad performance.. not a DOM reform, but just only a HTML push
Elaine
@Avinash @Elaine - Thanks for the feedback. I'm still learning jQuery myself, so I like to try to answer questions, but I'm not always there with the best implementation. Thanks for the correction (and good answer Avinash).
JasCav
Ninja Dude
@jason :) it's ok
Elaine
+2  A: 
$(function() {
    $('body').wrapInner('<div id="bodyContainer"/>');
    $('<div />',{id:"footer",text :"i'm a footer"})
        .insertAfter('#bodyContainer');
});

This should do the job, Here is the Demo : http://jsfiddle.net/DeNjE/

Ninja Dude
I think this is the right solution I want, thank a lot!sample -> http://jsfiddle.net/4AmdV/8/
Elaine