简体   繁体   中英

insert new element after previous added elements

JavaScript

$(".display_box").on('click', function () {
    var name = $(this).attr("data-name");
    var x = document.getElementById('added-people').innerHTML;
    $("name").insertAfter(x);
});

HTML

<div id="added-people" 
    style="margin-left:1%;
           min-height:15px;
           width:71.7%;
           background:#f2f4f5;
           font-family: 'lucida grande',tahoma,verdana,arial,sans-serif;
           font-size: 9px;color: #212121;">
</div>      

The display box is generated using a while loop. So, when a display box is clicked, its name is fetched and displayed in the 'added-people' div. I want that when a display box is clicked again, the fetched name should display next to the previous one and so on.

$(".display_box").on('click', function () {
    var name = $(this).data("name") // use .data() to grab data-name
    var x = $("#added-people") // $("#id") same as document.getElementById('id')
    x.append( '<span>' + name + '</span>' );
});

您可以使用jquery Append函数,该函数附加在您的情况下为var x的文本末尾

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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