0

我有 2 个与自己的相位一起工作的红绿灯。您可以在JSFiddle访问我的演示,或查看源代码:

$(function() {
    traffic_lights = [
        [{color:'green' ,time:5 },
        {color:'yellow' ,time:3 },
        {color:'red' ,time:5 },],

        [{color:'green' ,time:10 },
        {color:'yellow' ,time:6 },
        {color:'red' ,time:10 },],
    ]


    for (var a = 0, f; a < traffic_lights.length; a++) {
        var g = traffic_lights[a];
        f = $("#c" + a);
        (function (a, d) {
            var b = 0,
                time = 0;
            return function s() {
                time || (a.css({"background-color": d[b].color}), time = d[b].time, b = ++b % d.length);
                a.html(time);
                time--;
                window.setTimeout(s, 1e3);
            }
        })(f, g)();
    }

})

我如何知道正在处理哪个红绿灯 (span id) 以及它的颜色是什么?

4

1 回答 1

1

如果我做对了,跨度是f,所以它的 id 属性是"c" + a。您可以使用`f.css('background-color') 获取其背景颜色。

编辑:这个问题真的不清楚,你对哪个范围和生命周期感兴趣?您可能应该使用a而不是finside s。正如你已经在做的那样。所以我对你认为你有什么问题感到困惑。

于 2013-10-19T17:38:19.530 回答