简体   繁体   English

Javascript将div添加到另一个div的顶部

[英]Javascript add div to top of another div

I want to add a div element at the top inside another div element. 我想在另一个div元素的顶部添加一个div元素。

<div class="chat-box" id="chat-box">
    <div class="item">
        <img src="avatar.png"/>
        <p class="message">
        <a href="#" class="name"><small class="text-muted pull-right"><i class="fa fa-clock-o">Date</i></small></a>
        Message
        </p>
    </div>
</div>

I need to add new "item" divs above the ones that are already there. 我需要在已经存在的那些上面添加新的“item”div。 Is there any jquery or javascript snipped that does this for me? 是否有任何jquery或javascript snipped为我这样做?

Thanks for the help! 谢谢您的帮助! :) :)

.append() will append it at the end of #chat-box . .append()会将它附加到#chat-box的末尾。

.prepend() will pre -pend it, at the beginning of it, like what you're trying to achive. .prepend .prepend()会在它开始时预先添加它,就像你想要实现的那样。

var new_item_html = "<div class='item'>i am an item</div>"
$("#chat-box").prepend(new_item_html);

hope that helps. 希望有所帮助。

In general the div's display is in their order. 通常,div的显示顺序如下。 Any additional div will be the last on top. 任何额外的div将是最后一个。 Use in the div's style position:absolute 在div的样式位置使用:绝对

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

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