标签云

微信群

扫码加入我们

WeChat QR Code

This question already has an answer here:

I'm trying to learn JS and got an issue.

I tried many things and googled but all in vain. Following piece of code doesn't work as expected. I should get value of i on click but it always returns 6. I'm pulling my hair out., please help.

for (var i = 1; i < 6; i++) {

    console.log(i);

    $("#div" + i).click(
        function() {
            alert(i);
        }
    );
}

jsfiddle


You're dealing with a closure: stackoverflow.com/questions/111102/…

2018年07月17日06分56秒