简体   繁体   中英

jQuery Append codes doesn't append my divs

I was trying to:

  1. get the div.main-content (this is the main div)

  2. append from there

  3. then find img class avatar-main and put the img url there

  4. then get the h5 class fullName and put the fullName & the span class userNameMain and put the username there, & the timePosted class there and put the time posted

  5. then get the paragraph class tweetContent and put the text in there

  6. Finally I'll append a static uls with classes:

Here's my jQuery code:

$(document).ready(function(){
  ativeTweets();
});

function ativeTweets(){
  var index = streams.home.length - 1;
  while(index >= 0){
    var tweet = streams.home[index];
    var $tweet = $('div.main-content');
    $tweet.append('<div class="box-content"></div>');
    $tweet.append('<img src="" align="left" class="avatar-main">');
    $tweet.append('<h5 class="fullNameMain"> <span class="userNameMain"> <span class="timePosted">* 5h</span></span></h5>');
    $tweet.append('<p class="tweetContent"></p>');
    $tweet.append('<ul class="activities">
                <li><span class="comment"></span> 48K</li>
                <li><span class="retweet"></span> 50K</li>
                <li><span class="heart"></span> 100K</li>
                <li><span class="msg"></span> 22K</li>
              </ul>');
    $('.box-content').find('.avatar-main').attr('src', 'img/' + tweet.user + '.jpg');
    $('.box-content').find('.fullNameMain').text(tweet.user);
    $('.box-content').find('span.userNameMain').text('@' + tweet.user);
    $('.box-content').find('span.timePosted').text(new Date());
    $('.box-content').find('.tweetContent').text(tweet.message);
    index -= 1;
  }
}

I am not sure but for some reason, these codes won't work and it doesn't append anything at all on the div:

<div id="main-content"></div>

ERROR:

Uncaught SyntaxError: Invalid or unexpected token

Am i doing something wrong here? Please help!

You have a number of issues.

  • First you attempt to use class not id in code.
  • You cannot extend a string over multiple lines
  • You append to the div but look in the box. I put them in the box
  • You can also cache the box ref when you create it (not strictly needed)
  • You were using .text and overwriting the contained element

 $(function() { ativeTweets(); }); var streams = {}; streams.home= [{user:"one",message:"on mess"},{user:"two",message:"on two mess"}];//just to run locally function ativeTweets() { var scount = streams.home.length; var index = streams.home.length; while (index--) { var tweet = streams.home[index]; var $tweet = $('#main-content'); $tweet.append('<div class="box-content"></div>'); var mybox = $tweet.find('.box-content').last(); mybox.append('<img src="" align="left" class="avatar-main">'); mybox.append('<h5 class="fullNameMain"> <span class="userNameMain"> <span class="timePosted">* 5h</span></span></h5>'); mybox.append('<p class="tweetContent"></p>'); mybox.append('<ul class="activities">' + '<li><span class="comment"></span> 48K</li>' + '<li><span class="retweet"></span> 50K</li>' + '<li><span class="heart"></span> 100K</li>' + '<li><span class="msg"></span> 22K</li>' + '</ul>'); mybox.find('.avatar-main').attr('src', 'img/' + tweet.user + '.jpg'); mybox.find('.fullNameMain').prepend(tweet.user); mybox.find('span.userNameMain').prepend('@' + tweet.user); mybox.find('span.timePosted').text(new Date()); mybox.find('.tweetContent').text(tweet.message); } } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div id="main-content"></div> 

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