简体   繁体   English

不从 JQuery object 中提取任何数据

[英]Not pulling any data from JQuery object

$(document).ready(function(){
var $body = $('body');

var index = streams.home.length - 1;
while(index >= 0){
  var tweet = streams.home[index];
  var $tweet = $('<div class = tweet></div>');
  var $user = $('<div class = users></div>');
  var $message = $('<div class = message></div>');
  var $time = $('<div class = time></div>');
  // $tweet.text('@' + tweet.user + ': ' + tweet.message + ' ' + tweet.created_at);
  $tweet.appendTo($('.tweets'));
  $time.text(tweet.created_at + '\n').appendTo($tweet);
  $user.text('@' + tweet.user + ': ').attr('username', tweet.user).appendTo($tweet);
  $message.text(tweet.message + ' ').appendTo($tweet);
  index -= 1;
}

//see user history by clicking on name
  //click event on name element
  //hide all other users that do not have the same username attribute?
$('.tweets').on('click', '.users', () => {
  var user = $(this).data('users');
  console.log(user);
})

So I'm trying to pull data from a class when I click on it.所以当我点击它时,我试图从 class 中提取数据。 This involves the last few lines of my code.这涉及我代码的最后几行。 The data stored in my .users should give me an output of {someName: 'stringOfName"} however when I click on it I get an empty object {} . What am I doing wrong? I'm adding data to my .users and I can clearly see it being displayed holding information so am I pulling the data from this object incorrectly?存储在我的.users中的数据应该给我一个{someName: 'stringOfName"}但是当我点击它时,我得到一个空的 object {} 。我做错了什么?我正在将数据添加到我的.users和我可以清楚地看到它正在显示持有信息,所以我从这个 object 中提取数据是否不正确?

$(this).data('users'); would get info from a data-attribute called "users" on the clicked element.将从单击元素上称为“用户”的数据属性中获取信息。 But I don't see anywhere in you code where you attach any data-attributes to any of your elements.但是我在您的代码中没有看到您将任何数据属性附加到您的任何元素的任何地方。 You've added a "username" attribute, but that's not the same as a data-attribute, and it also has a different name.您添加了一个“用户名”属性,但这与数据属性不同,它也有不同的名称。

Secondly, you can't use an arrow function as your "click" callback function because this will have the wrong scope.其次,您不能使用箭头 function 作为“点击”回调 function,因为this将有错误的 scope。 (You can read more about this elsewhere online ). (您可以在网上其他地方阅读更多相关信息)。

Here's a working demo:这是一个工作演示:

 $(document).ready(function() { //some dummy data var streams = { "home": [{ "user": "a", "message": "hello", "created_at": "Friday" }] }; var index = streams.home.length - 1; while (index >= 0) { var tweet = streams.home[index]; var $tweet = $('<div class="tweet"></div>'); var $user = $('<div class="users"></div>'); var $message = $('<div class="message"></div>'); var $time = $('<div class="time"></div>'); // $tweet.text('@' + tweet.user + ': ' + tweet.message + ' ' + tweet.created_at); $tweet.appendTo($('.tweets')); $time.text(tweet.created_at + '\n').appendTo($tweet); //create a data-attribute instead of an attribute $user.text('@' + tweet.user + ': ').data('username', tweet.user).appendTo($tweet); $message.text(tweet.message + ' ').appendTo($tweet); index -= 1; } //use a regular function insted of an arrow function $('.tweets').on('click', '.users', function() { var user = $(this).data('username'); //search for the correct data-attribute name console.log(user); }) });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="tweets"></div>

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

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