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

I want to add a div element at the top inside another div element.<div class="chat-box" id

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745617510a4636324.html

相关推荐

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

    I want to add a div element at the top inside another div element.<div class="chat-box" id

    13小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信