最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Javascript add div to top of another div - Stack Overflow

programmeradmin1浏览0评论

I want to add a div element at the top inside another div element.

<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. Is there any jquery or javascript snipped that does this for me?

Thanks for the help! :)

I want to add a div element at the top inside another div element.

<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. Is there any jquery or javascript snipped that does this for me?

Thanks for the help! :)

Share Improve this question asked Mar 16, 2014 at 13:50 user3425765user3425765 2911 gold badge4 silver badges12 bronze badges 1
  • .prepend() – Patrik Krehák Commented Mar 16, 2014 at 13:53
Add a ment  | 

2 Answers 2

Reset to default 5

.append() will append it at the end of #chat-box.

.prepend() will pre-pend it, at the beginning of it, like what you're trying to achive.

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. Any additional div will be the last on top. Use in the div's style position:absolute

发布评论

评论列表(0)

  1. 暂无评论