【发布时间】:2011-05-13 17:22:21
【问题描述】:
我的 .length() 属性有点问题。我正在尝试计算每个 div 中具有相同类的跨度数。这些 Div 先前已附加在脚本中。有趣的是,如果我警告与长度相对应的变量,一切正常,如果我删除警告,它不会。以前从未见过,所以任何帮助将不胜感激:) 这是代码:
getTweets();
tweetCounter = function(){
$('.entry').each(function(){
var nTweets = $('.tweet', this).length;
var infoPane = $('.infoPane', this);
var tLink = '<a href="#" class="tLink" title="Tweets of the day">' + nTweets + ' Tweets that day</a>';
infoPane.append(tLink);
//alert(nTweets);
});
}
tweetCounter();
正如我所说,当我取消注释警报时,它会正确附加。如果有注释,每个 DIV 上都会显示 0... 有什么想法吗?
这是 getTweets 函数:
getTweets = function(){
var url = "http://twitter.com/status/user_timeline/charleshaa.json?count=30&callback=?";
$.getJSON(url, function(data){
$.each(data, function(i, item) {
var fullTDate = item.created_at;
var splitTDate = fullTDate.split(" ");
var tMonth = splitTDate[1];
if (tMonth == "Jan"){
tMonth = "01"
} else if(tMonth == "Feb"){
tMonth = "02"
} else if(tMonth == "Mar"){
tMonth = "03"
} else if(tMonth == "Apr"){
tMonth = "04"
} else if(tMonth == "May"){
tMonth = "05"
} else if(tMonth == "Jun"){
tMonth = "06"
} else if(tMonth == "Jul"){
tMonth = "07"
} else if(tMonth == "Aug"){
tMonth = "08"
} else if(tMonth == "Sep"){
tMonth = "09"
} else if(tMonth == "Oct"){
tMonth = "10"
} else if(tMonth == "Nov"){
tMonth = "11"
} else if(tMonth == "Dec"){
tMonth = "12"
}
var tDay = splitTDate[2];
var tYear = splitTDate[5];
var tDate = tDay + '-' + tMonth + '-' + tYear;
var tText = '<span class="tweet">' + item.text + '</span>';
//alert(tDate);
var destination = $('#date_'+ tDate +'');
destination.append(tText);
});
});
}
【问题讨论】:
-
jQuery中没有
.length()这样的函数。length只是一个 javascript 数组的属性。 -
@TheSuperTramp:是的。 jQuery 对象的行为类似于类数组对象,因此拥有
.length属性。 -
这正是我所说的。你可以写
$('div').length,但不能写$('div').length()