Javascript move div for end body </body> tag

How to move div to end tag </body> . I use openx, I cannot change the contents of a div because the content belongs to others. I just add javascript from openx in order to get a banner, I put the code after the <body> before marking the tag in the content. And my problem is that when I add javascript from openx to create a banner, I get two divs in parallel, which banner in <div id="div1"> and <div id="div2"> is in the tag below <body> , I want <div id="div1"> after the start of the <body> tag above the anyting tag in the content. And <div id="div2"> before the end body </body> tag after the anyting tag.

This 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 go to the following:

 <!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 a banner above the content and the banner two below the content. I do not want to use CSS position: fixed .

So, is it possible to move the div tag to the body of the tag tag? If possible, please help me write javascript code to do this.

Thanks.

+4
source share
2 answers

Plain simple javascript:

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

To move a node, you simply use the appendChild method.

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

+8
source

Yes. You can do it, but you need jQuery .

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

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


The usual JavaScript method.

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

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


From the comments, this can be done in a more efficient way specified by dfsq :

 document.body.appendChild(document.getElementById('div2')); 
+4
source

All Articles