admin管理员组

文章数量:1191726

How to move div to before tag end body </body>. I'm using openx, I cannot change div content, because the content owned by others. I just put code javascript from openx in order to get banner I put the code after tag <body> before anyting tag in content. And my problem is, when I put javascript from openx to generate banner I get two div parallel, which the banner in <div id="div1"> and <div id="div2"> there are in below tag <body>, I want to <div id="div1"> after tag start body <body> above anyting tag in content. And <div id="div2"> in before tag end body </body> after anyting tag.

This is I get html from openx, as below:

<!DOCTYPE html>
<html>
    <head>
    </head>
    <body>
        <div id="div1"><img src="blablabla" /></div>
        <div id="div2"><img src="blablabla" /></div>
        Here is content other div id or div class,
        I not define example insert after div id footer
        Because this is content owned by others.
        This is justexample content.
    </body>
</html>

and I want to transform to as below:

<!DOCTYPE html>
<html>
    <head>
    </head>
    <body>
        <div id="div1"><img src="blablabla" /></div>
        Here is content other div id or div class,
        I not define, example insert after div id footer
        Because this is content owned by others.
        This is justexample content.
        <div id="div2"><img src="blablabla" /></div>
    </body>
</html>

Because I want to put banner one above content and banner two to below content. I don't want to use CSS position: fixed.

So, possible to move tag div to before tag end body? If possible, please help me code javascript to do it.

Thanks.

How to move div to before tag end body </body>. I'm using openx, I cannot change div content, because the content owned by others. I just put code javascript from openx in order to get banner I put the code after tag <body> before anyting tag in content. And my problem is, when I put javascript from openx to generate banner I get two div parallel, which the banner in <div id="div1"> and <div id="div2"> there are in below tag <body>, I want to <div id="div1"> after tag start body <body> above anyting tag in content. And <div id="div2"> in before tag end body </body> after anyting tag.

This is I get html from openx, as below:

<!DOCTYPE html>
<html>
    <head>
    </head>
    <body>
        <div id="div1"><img src="blablabla" /></div>
        <div id="div2"><img src="blablabla" /></div>
        Here is content other div id or div class,
        I not define example insert after div id footer
        Because this is content owned by others.
        This is justexample content.
    </body>
</html>

and I want to transform to as below:

<!DOCTYPE html>
<html>
    <head>
    </head>
    <body>
        <div id="div1"><img src="blablabla" /></div>
        Here is content other div id or div class,
        I not define, example insert after div id footer
        Because this is content owned by others.
        This is justexample content.
        <div id="div2"><img src="blablabla" /></div>
    </body>
</html>

Because I want to put banner one above content and banner two to below content. I don't want to use CSS position: fixed.

So, possible to move tag div to before tag end body? If possible, please help me code javascript to do it.

Thanks.

Share Improve this question asked Mar 27, 2013 at 18:00 Loren RamlyLoren Ramly 1,1114 gold badges13 silver badges22 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 22

Plain simple javascript:

document.body.appendChild(document.getElementById('div2'));

To move the node you just use appendChild method.

And the demo http://jsfiddle.net/6GsbB/

Yes. You can do this way, but you need jQuery.

a = $("#div2").clone();
$("#div2").remove();
$("body").append(a);

Fiddle: http://jsfiddle.net/praveenscience/zc8Ze/


Plain JavaScript method.

var a = document.getElementById("div2");
document.body.appendChild(a);

Fiddle: http://jsfiddle.net/praveenscience/zc8Ze/1/


From the comments, this can be made in a better efficient way, given by dfsq:

document.body.appendChild(document.getElementById('div2'));

本文标签: Javascript move div to tag end body ltbodygtStack Overflow