简体   繁体   English

Javascript将div移动到标记结束正文</body>

[英]Javascript move div to tag end body </body>

How to move div to before tag end body </body> . 如何在标记结束体</body>之前将div移动到 I'm using openx, I cannot change div content, because the content owned by others. 我正在使用openx,我无法更改div内容,因为其他人拥有的内容。 I just put code javascript from openx in order to get banner I put the code after tag <body> before anyting tag in content. 我只是从openx中输入代码javascript以获得横幅我在标记<body>之前将代码放在内容中的任何标记之前。 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. 我的问题是,当我从openx生成javascript生成横幅时,我得到两个div并行,其中<div id="div1"><div id="div2">中的横幅位于下面的标记<body> ,我想在标签开始体<body>之后<div id="div1">在内容中的任何标签之上。 And <div id="div2"> in before tag end body </body> after anyting tag. 并且在任何标签之后</body> tag <div id="div2">在标签结束体</body>之前。

This is I get html from openx, as below: 这是我从openx获取html,如下所示:

<!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 . 我不想使用CSS position: fixed

So, possible to move tag div to before tag end body? 那么,可以将标签div移动到标签结束体之前吗? If possible, please help me code javascript to do it. 如果可能的话,请帮我编写javascript代码。

Thanks. 谢谢。

Plain simple javascript: 简单的简单javascript:

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

To move the node you just use appendChild method. 要移动节点,只需使用appendChild方法即可。

And the demo http://jsfiddle.net/6GsbB/ 和演示http://jsfiddle.net/6GsbB/

Yes. 是。 You can do this way, but you need jQuery . 你可以这样做,但你需要jQuery

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

Fiddle: http://jsfiddle.net/praveenscience/zc8Ze/ 小提琴: http//jsfiddle.net/praveenscience/zc8Ze/


Plain JavaScript method. 简单的JavaScript方法。

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

Fiddle: http://jsfiddle.net/praveenscience/zc8Ze/1/ 小提琴: http//jsfiddle.net/praveenscience/zc8Ze/1/


From the comments, this can be made in a better efficient way, given by dfsq : 根据评论,这可以通过dfsq以更有效的方式进行:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM