首页 > 关于闭包问题

关于闭包问题

在javascript语言精髓一书中看到此段关于闭包的代码:

var fade = function(node) {
    var level = 1;
    var step = function() {
        var hex = level.toString(16);
        node.style.backgroundColor = '#FFF' + hex + hex;
        if(level < 15) {
            level += 1;
            setTimeout(step, 100);
        }
    };
    setTimeout(step, 100);
};
fade(document.body);

应该如何对其进行修改才能有效。


这段代码运行是有效的呀,可以把body的背景色从#FFFF11改到#FFFFFF,题主能具体说说你的需求吗?


这句错了,颜色的长度应是 3 或是 6 这个是 5

node.style.backgroundColor = '#FFF' + hex + hex;

可以写

node.style.backgroundColor = '#FFFF' + hex + hex;
// 或者
node.style.backgroundColor = '#' + hex + hex + hex;
【热门文章】
【热门文章】