关于闭包问题

在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);

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

阅读 2.9k
2 个回答

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

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

可以写

node.style.backgroundColor = '#FFFF' + hex + hex;
// 或者
node.style.backgroundColor = '#' + hex + hex + hex;

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

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题