tags:

views:

37

answers:

1

I want to perform the sticky footer to a stand-for-a-long web project. I'm required to create two top-level div and wrap up all the content inside of <body> into 'div-container', then to add 'div-footer' to the end of <body> or <html> like

<body>
<div id='container'>
//all body 
</div>
<div id='footer'>
</div>
</body>

How can i do it with jQuery? i tried but failed with the code below (coz 'container' don't know where to append),

$(document).ready(function() {
    addStikyFooter();
});

function addStikyFooter() {
    $("<div id='container'></div>").append($('body'));
    var footerHtml = "<div id='footer'>i'm testing!/footer>";
    alert($('body').html());
}
+2  A: 

Use append:

    $("body").append("<div id='footer'></div>");

Append will add your code at the end, but within the specified element. So $("body").append() will add things within the body.

In your code it looks like you switched the order of the content and the element to append to, so your code should read:

$(document).ready(function() {
    addStikyFooter();
});

function addStikyFooter() {
    $("body").append("<div id='container'></div>");
    $("body").append("<div id='footer'>i'm testing!</div>");
}

Updated example

Peter Ajtai
hi, thanks, i updated my topic, please help check again, i first need to embed all the body content into a div-container. footer is okay for me. not a challenge.
Elaine
good, it works, looks good, har :)
Elaine
I ever used the code you just posted, but 'alert($('body').html())', shows div all appended to the end of body-content, so I thought i failed.. anyway, it works :)
Elaine
forgot to say thanks ^^
Elaine
You're welcome. Append() will add things at the end of but within the specified element, so the alert box here is showing everything that is now inside the body ==> http://jsfiddle.net/k9ybH/
Peter Ajtai
ya.. that's it. it looks like just append to the end, but not embedding the body content.
Elaine
hi, Perter, you there? I just found this kinda eventually turns out to be <html> <div id='container'> <body>..</body> </div> <div id='footer'> </div> </html>not what I wanted.. this blocks horizontal-scrollbar, i don't know whyPlease try here, http://jsfiddle.net/7szM4/1/
Elaine
@Elaine - The code you linked is not doing what I described in my answer. It is very different. I edited it ==> http://jsfiddle.net/yq5FK/ . The edited code does do what I described in the answer - If you use Firebug, you can check that's true ==> http://i.imgur.com/pzwDc.jpg
Peter Ajtai