Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/470.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript将div移动到标记结束体</车身>;_Javascript - Fatal编程技术网

Javascript将div移动到标记结束体</车身>;

Javascript将div移动到标记结束体</车身>;,javascript,Javascript,如何将div移动到标记结束正文之前。我使用的是openx,我无法更改div内容,因为其他人拥有这些内容。我只是把openx中的代码javascript放在内容中的标记之后的任何标记之前,以获得横幅。 我的问题是,当我从openx中使用javascript生成横幅时,我得到了两个并行的div,中的横幅和中的横幅位于标记的下方,我想在标记开始体之后,在内容中的任何标记上方。和在标签末端主体前在任意标签后 这是我从openx获得的html,如下所示: <!DOCTYPE html> <

如何将div移动到标记结束正文之前
。我使用的是openx,我无法更改div内容,因为其他人拥有这些内容。我只是把openx中的代码javascript放在内容中的标记
之后的任何标记之前,以获得横幅。 我的问题是,当我从openx中使用javascript生成横幅时,我得到了两个并行的div,
中的横幅和
中的横幅位于标记
的下方,我想
在标记开始体
之后,在内容中的任何标记上方。和
在标签末端主体前
在任意标签后

这是我从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>
<!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>

这是其他div id或div类的内容,
我没有定义在div id页脚后插入的示例
因为这是其他人拥有的内容。
这只是示例内容。
我想转换为如下所示:

<!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>
<!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>

这是其他div id或div类的内容,
我没有定义,示例在div id页脚后插入
因为这是其他人拥有的内容。
这只是示例内容。
因为我想把横幅一放在内容上面,横幅二放在内容下面。我不想使用CSS
position:fixed

那么,是否可以在标记结束主体之前将标记div移动到? 如果可能的话,请帮我编写javascript代码


谢谢。

是的。你可以这样做,但你需要

小提琴:
纯JavaScript方法

var a = document.getElementById("div2");
document.body.appendChild(a);
小提琴:
从评论中可以看出,这是一种更有效的方式,如下所示:


简单javascript:

document.body.appendChild(document.getElementById('div2'));
要移动节点,只需使用方法


演示不需要克隆,只需移动元素即可,这样可以节省内存:
$('body').append($('div2'))
OP没有要求jQuery。。。我希望你不会建议他包括、下载、评估一个80kb的库,只是为了
appendChild
?您好,谢谢您的回复,没有jquery,因为它会生成到其他网站,而该网站属于其他网站,那么如果该网站没有找到jquery该怎么办。谢谢。@PraveenKumar当然,我现在就把它取下来;)此外,您不需要创建两个引用并调用
removeChild
。我个人更喜欢@dfsq的答案。无论如何,你的代码是正确的,我会给你+1。加上jQuery版本。您好,谢谢您的回复,我在控制台日志中收到错误未捕获错误:NotFoundError:DOM异常8,代码在上面它不适用于您,因为
document.getElementById('div2')
返回
null
。确保您传递了正确的id,并且您的HTML中确实有它。hwhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh。现在,您的代码已经解决了这个问题。谢谢你,伙计。