[英].eq() argument not working in loop
I have the following function: 我有以下功能:
function createLogos() {
for (var l=0; l<userList().length; l++) {
var username = "https://api.twitch.tv/kraken/users/" + userList()[l];
$.getJSON(username, function(data) {
$(":eq(l)").append("<img src=" + data.logo +">");
});
}
}
However, the eq(l)
is not recognising what l is. 但是,
eq(l)
没有认识到l是什么。 Replacing it with a number and it works as to how I wish. 用数字代替它,它可以按照我的意愿工作。
Can anyone see why it might be behaving like this? 任何人都可以看到为什么它可能会像这样?
You can substitute $.map()
for for
loop, use $.when()
您可以将
$.map()
替换为for
循环,使用$.when()
function createLogos() {
$.when.apply($, $.map(userList(), function(el, index) {
var username = "https://api.twitch.tv/kraken/users/" + el;
return $.getJSON(username, function(data) {
$("#theTable .pic").eq(index).append("<img src=" + data.logo +">");
})
}))
}
In your code you are looking for index l
not the current index of the loop. 在您的代码中,您正在寻找索引
l
而不是循环的当前索引。 Now id you were do do it by referencing the variable you will have a different problem since l will be evaluated when the code is hit so it will be the wrong value. 现在id你是通过引用变量来做的,你将遇到一个不同的问题,因为当代码被命中时我将被评估,因此它将是错误的值。
So what you need to be is reference the element before the JSON call 所以你需要的是在JSON调用之前引用该元素
function createLogos() {
for (var l=0; l<userList().length; l++){
var username = "https://api.twitch.tv/kraken/users/" + userList()[l],
pic = $("#theTable .pic").eq(l);
$.getJSON(username, function(data){
pic.append("<img src=" + data.logo +">");
});
}
}
Try to concatenate :eq()
尝试连接
:eq()
Hope it will work 希望它会奏效
function createLogos() {
for (var l=0; l<userList().length; l++){
var username = "https://api.twitch.tv/kraken/users/" + userList()[l];
$.getJSON(username, function(data){
$(":eq("+l+")").append("<img src=" + data.logo +">");
})
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.