$(function() {
$.get('/viewonline',function(data) {
var members = $('a.username');
var location = $('a.location');
var elems = $(data);
for (var i=0; i<members.length;i++) {
var uname = members[i].text();
var aname = members[i].href;
$('.userlist_online').html('<a href="'+ aname +'">'+ uname +'</a>');
}
for (var j=0;j<location.length;j++) {
var locaname= location[j].text();
var locaaname = location[j].href;
$('.userlist_views').html('<a href="'+ locaaname +'">'+ locaname +'</a>');
}
},'html');
});
上面的代码我尝试使用a hrefs
和username
类来遍历所有location
。所以上面的代码不会抛出任何错误,但它不会将任何数据发布到div中。
任何人都可以帮我弄清楚我做错了什么?
答案 0 :(得分:2)
以下是我对可行方法的猜测:
$(function () {
$.get('/viewonline', function (data) {
data = $(data);
var members = data.find('a.username');
var location = data.find('a.location');
for (var i = 0; i < members.length; i++) {
var uname = $(members[i]).text();
var aname = members[i].href;
$('.userlist_online').append('<a href="' + aname + '">' + uname + '</a>');
}
for (var j = 0; j < location.length; j++) {
var locaname = $(location[j]).text();
var locaaname = location[j].href;
$('.userlist_views').append('<a href="' + locaaname + '">' + locaname + '</a>');
}
}, 'html');
});
修复完成:
members
和location
。 $()
text()
包装元素
append
代替html