Closed. This question is not reproducible or was caused by typos。它当前不接受答案。
                        
                    
                
            
        
            
        
                
                    
                
            
                
                    想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
                
                    4年前关闭。
            
        

    

我试图实现的目标是做到这一点,以使我在单击用户时仅能看到其推文。

由于某些原因,我在运行代码时会不断收到这些奇怪的字符。这是chrome开发工具中怪异人物的照片:
javascript - 不确定为什么我会不断收到这些奇怪的字符吗?试图查看特定用户的推文-LMLPHP
我指的是div class =“之后的字符

我相信这与以下行有关:



var $tweet = $('<div class=“user" data-user="' + tweet.user + '"></div>');





这是我的代码:



    <script>
      $(document).ready(function(){

        var globalVar;

        var makeTweets = function(){
          var $body = $('.tweet-display');
          $body.html('');

          var index = streams.home.length - 1;
          while(index >= 0){
            var tweet = streams.home[index];
            var $tweet = $('<div class=“user" data-user="' + tweet.user + '"></div>');
            $tweet.text('@' + tweet.user + ': ' + tweet.message);
            if (globalVar === undefined) {
             $tweet.appendTo($body);
            } else if(globalVar===tweet.user){
              $tweet.appendTo($body);
            }
            index -= 1;
          }
        }
        makeTweets();
        setInterval(function() {
          makeTweets();
        }, 1000);
      });

    </script>
  </body>
</html>

<!DOCTYPE html>
<html>
  <head>
    <link rel="stylesheet" type="text/css" href="styles.css">
    <script src="jquery.js"></script>
    <script src="data_generator.js"></script>
  </head>
  <body>
  <div class="navbar">
    <div id="navbar-text">
    Twittler Project
      <!-- <div class="button new-tweet">New Tweet</div>
      <div class="button refresh">Refresh</div>
      <div class="button ">Button</div> -->
    </div>
    <div id="twittler-bird">
      <a href="http://imgur.com/zuTfb54"><img src="http://i.imgur.com/zuTfb54.png" title="source: imgur.com" style="height:100px;" /></a>
    </div>
  </div>
  <!-- <div class="tweet-inside">test</div> -->
  <div class="tweet-container">
    <div class="tweet-display">
    </div>
  </div>
  <div class="sidebar-right">
    <div class="button tweet-new"></div>
    <div class="button"></div>
  </div>





以防万一,这是一些与之配对并链接在一起的javascript:



// set up data structures
window.streams = {};
streams.home = [];
streams.users = {};
streams.users.shawndrost = [];
streams.users.sharksforcheap = [];
streams.users.mracus = [];
streams.users.douglascalhoun = [];
window.users = Object.keys(streams.users);

// utility function for adding tweets to our data structures
var addTweet = function(newTweet){
  var username = newTweet.user;
  streams.users[username].push(newTweet);
  streams.home.push(newTweet);
};

// utility function
var randomElement = function(array){
  var randomIndex = Math.floor(Math.random() * array.length);
  return array[randomIndex];
};

// random tweet generator
var opening = ['just', '', '', '', '', 'ask me how i', 'completely', 'nearly', 'productively', 'efficiently', 'last night i', 'the president', 'that wizard', 'a ninja', 'a seedy old man'];
var verbs = ['drank', 'drunk', 'deployed', 'got', 'developed', 'built', 'invented', 'experienced', 'fought off', 'hardened', 'enjoyed', 'developed', 'consumed', 'debunked', 'drugged', 'doped', 'made', 'wrote', 'saw'];
var objects = ['my', 'your', 'the', 'a', 'my', 'an entire', 'this', 'that', 'the', 'the big', 'a new form of'];
var nouns = ['cat', 'koolaid', 'system', 'city', 'worm', 'cloud', 'potato', 'money', 'way of life', 'belief system', 'security system', 'bad decision', 'future', 'life', 'pony', 'mind'];
var tags = ['#techlife', '#burningman', '#sf', 'but only i know how', 'for real', '#sxsw', '#ballin', '#omg', '#yolo', '#magic', '', '', '', ''];

var randomMessage = function(){
  return [randomElement(opening), randomElement(verbs), randomElement(objects), randomElement(nouns), randomElement(tags)].join(' ');
};

// generate random tweets on a random schedule
var generateRandomTweet = function(){
  var tweet = {};
  tweet.user = randomElement(users);
  tweet.message = randomMessage();
  tweet.created_at = new Date();
  addTweet(tweet);
};

for(var i = 0; i < 10; i++){
  generateRandomTweet();
}

var scheduleNextTweet = function(){
  generateRandomTweet();
  setTimeout(scheduleNextTweet, Math.random() * 1500);
};
scheduleNextTweet();

// utility function for letting students add "write a tweet" functionality
// (note: not used by the rest of this file.)
var writeTweet = function(message){
  if(!visitor){
    throw new Error('set the global visitor property!');
  }
  var tweet = {};
  tweet.user = visitor;
  tweet.message = message;
  addTweet(tweet);
};

最佳答案

您在之前的这一行中使用了错误的引号("而不是user):

var $tweet = $('<div class=“user" data-user="' + tweet.user + '"></div>');


我认为这可能是问题所在。

关于javascript - 不确定为什么我会不断收到这些奇怪的字符吗?试图查看特定用户的推文,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36195699/

10-10 00:10