简体   繁体   English

JavaScript-onload事件范围

[英]JavaScript - scope on onload event

I'm new to Javascript and I cannot find what's wrong in my code... Hope you can help me :) 我是Java语言的新手,我无法在代码中找到问题所在...希望您能为我提供帮助:)

    function addchannel(channel, program) {
            $('#channel-list').append("<li><h2>" + channel.name + " " program.response.name + "</h2></li>");
    };

    var xhr = new XMLHttpRequest({ mozSystem: true });
    xhr.open("GET", "...");
    xhr.responseType="json";
    xhr.send();
    xhr.onload = function(){

        var channels = xhr.response;
        for (var i=0; i<channels.length; i++) {

            xhr2 = new XMLHttpRequest({ mozSystem: true });
            xhr2.open("GET", "..."+channels[i]+"...");
            xhr2.responseType="json";
            xhr2.send();
            xhr2.onload = addchannel(channels[i], xhr2); //HERE IS THE PROBLEM...
        }


    }

I think I have problems with xhr2.onload function (addchannel) that cannot see xhr2... I tried with "this" or something else, but nothing works... 我认为xhr2.onload函数(addchannel)存在问题,无法看到xhr2 ...我尝试使用“ this”或其他方法,但没有任何效果...

Thank you :) 谢谢 :)

function addchannel(channel, program) {
        $('#channel-list').append("<li><h2>" + channel.name + " " program.response.name + "</h2></li>");
};

I don't see any issues with scope, but you're missing a plus sign in the addchannel function (" " + program.response) ... 我没有看到作用域的任何问题,但是您在addchannel函数(“” + program.response)中缺少加号...

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

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