Event handlers inside a Javascript loop - need a closure?

后端 未结 1 1800
夕颜
夕颜 2020-11-22 08:48

I\'m working with a bit of html and Javascript code that I\'ve taken over from someone else. The page reloads a table of data (via an asynchronous request) every ten seconds

相关标签:
1条回答
  • 2020-11-22 09:15

    You do, indeed, need to implement a closure here. This should work (let me know - I didn't test it)

    var blah = xmlres.getElementsByTagName('blah');
    for(var i = 0; i < blah.length; i++) {
        var td = document.createElement('td');
        var select = document.createElement('select');
        select.setAttribute("...", "...");
        select.onchange = function(s,c,a)
        {
            return function()
            {
                onStatusChanged(s,c,a);
            }
        }(select, callid, anotherid);
        td.appendChild(select);
    }
    
    0 讨论(0)
提交回复
热议问题