我的jquery循环出现问题。容器刷新后,它会不断添加相同的条目。代码是......
$(function() {
$('<div />').html('<div id="buddies" class="buddies" style="vertical-align:bottom"><div class="imheader"> Online Favorites<span class="fr"><a href="#" id="exitlist">X</a></span></div><div class="imheader2"><div class="imheaderspan"><a href="#">Options</a></div></div><ul class="imwindow"><div id="myonlinefavs"></div></ul></div><div id="chatbar" class="chatbar"><div id="ims"></div><div class="buddylist" id="buddylist"><center><a href="#" id="im-menu" class="im-menu">Favorites (<strong id="friendsonline">0</strong>)</a></center></div></div>').appendTo('body');
var imRoot = $("#im-menu");
var buddies = $("#buddies");
imRoot.click(function(){
buddies.toggle();
imRoot.blur();
return( false );
});
$("#exitlist").click(function(){
buddies.toggle();
imRoot.blur();
return( false );
});
$(document).click(function( event ){
if (buddies.is( ":visible" ) && !$( event.target ).closest( "#buddies" ).size()){
buddies.hide();
}
});
$(function FL(){
$.ajax({
type: 'POST',
url: 'config/receive_buddylist.php',
cache: 'false',
dataType: 'json',
success: function(data){
onlineNumber = buddylistreceived = 0;
data && $.each(data, function(i, e){
if (i == "buddylist") {
buddylistreceived = 1;
totalFriendsNumber = onlineNumber = 0;
$.each(e, function (l, f) {
if(f.id != null){
longname = f.n.length > 16 ? f.n.substr(0, 16) + "..." : f.n;
$('#myonlinefavs').append('<li>' + longname + '</li>');
}
if(f.s == '0') onlineNumber++;
totalFriendsNumber++;
});
}
});
R = onlineNumber;
if (R > 0){
$('#friendsonline').html(onlineNumber);
} else {
$('#myonlinefavs').html('No favorites online.');
$('#friendsonline').html('0');
}
}
});
Z = setTimeout(function () {
FL()
}, 1000)
});
});