简体   繁体   English

使用jQuery将数据追加到div

[英]Append data to div using jquery

Can I know how can we append the title and message to the div using jquery for every row? 我能知道如何使用每行的jquery将标题和消息附加到div吗? The issue with my code is that each of the data is not being displayed in a row. 我的代码的问题是每个数据都没有显示在一行中。

<!--
    For instance: Title1
                  Message1 
                  Title2
                  Message2   -->

<div class="widget-box">
     <div class="widget-title bg_lo"  data-toggle="collapse" href="#collapseG3" > <span class="icon"> <i class="icon-chevron-down"></i> </span>
        <h5>Topic</h5>
      </div>
      <div id="announcement" class="widget-content nopadding updates collapse in" id="collapseG3">
        <div class="new-update clearfix">
          <div class="update-done"><strong id ="title"><!-- post.title --></strong></a> <span id ="message"><!-- post.message --></span> </div>
        </div>
    </div>
</div>

<script>
     $.ajax({
         url: '/test/back-api/admin/announcements',
         method: 'GET',
         success: function(d){
         if(d.result){
             var posts = d.data;
             for(var i=0; i < posts.length; i++){
             var post = posts[i];
             $('#announcement').append(post.title, post.message);            
             }
         }else{
             $('#announcement').append("<div> </div>");
         }
         }
     });
</script>

Here is a snippet showing the use case 这是显示用例的代码片段

SNIPPET SNIPPET

 var message_container = '<div class="new-update clearfix"><div class="update-done"><strong id ="title"><!-- post.title --></strong></a> <span id ="message"><!-- post.message --></span> </div></div>'; $("#add").on('click',function(){ var message= $(message_container); var post = { title: "<h1>title</h1>", message: "<p>message</p>"}; message.find('#title').append(post.title); message.find('#message').append(post.message); $("#announcement").append(message); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="widget-box"> <div class="widget-title bg_lo" data-toggle="collapse" href="#collapseG3" > <span class="icon"> <i class="icon-chevron-down"></i> </span> <h5>Topic</h5> </div> <div id="announcement" class="widget-content nopadding updates collapse in" id="collapseG3"> </div> </div> <button id="add">Add</button> 

You can do the same thing in ajax 你可以在ajax中做同样的事情

var message_container = '<div class="new-update clearfix"><div class="update-done"><strong id ="title"><!-- post.title --></strong></a> <span id ="message"><!-- post.message --></span> </div></div>';


$.ajax({
      url: '/test/back-api/admin/announcements',
      method: 'GET',
      success: function(d) {
        if (d.result) {
          var posts = d.data;
          for (var i = 0; i < posts.length; i++) {
            var post = posts[i];

            var message = $(message_container);

            message.find('#title').append(post.title);
            message.find('#message').append(post.message);
            $("#announcement").append(message);
          }
 });

Looks to me like your markup isn't going to work very well if you're working with a collection of records. 在我看来,如果您正在处理记录集合,那么标记将无法很好地工作。 What you're likely to end up with the way you're going is something like... 您最终可能会遇到的事情是...

<!-- Title1
     Title2
     Message1
     Message2 -->

Change your code up a little so that you can add to the markup for each record: 稍微更改代码,以便可以为每个记录添加标记:

 $.ajax({
     url: '/test/back-api/admin/announcements',
     method: 'GET',
     success: function(d){
     if(d.result){
         var posts = d.data;
         for(var i=0; i < posts.length; i++){
         var post = posts[i];

         var Title = "<div class=\"title\">" + post.title + "</div>";
         var Message = "<div class=\"message\">" + post.message + "</div>";
         $('#announcement').append(Title, Message);
     }else{
         $('#announcement').append("<div> </div>");
     }
 });

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

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