遍历数组并设置变量 [英] Looping through an array and setting variables

查看:106
本文介绍了遍历数组并设置变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止我所拥有的:

What I have so far:

http://codepen.io/anon/pen/umHzl?editors=101

您会注意到您可以单击一个框,然后将其取消单击.我想要的是,当单击某个按钮时,所有其他按钮都未单击(恢复为正常颜色).

You notice that you can click a box and unclick it. What I would like is that when a certain button is clicked all other buttons are unclicked(turn back to normal color).

我对此的尝试:

        for (var i =0; i < booths.length; i++){
                var obj = booths[i]
                obj.e1['fill'] = obj['color'];
                obj.e1['checked'] = 'false';
                $("#"+obj.name).remove();
            }

我知道颜色在盒子的e1/rectangle对象中,但是我不知道如何更改/访问该变量.它说obj.e1是未定义的.如果我obj['fill']仍然不起作用.

I know that the color is in the e1/rectangle object of the box, but I do not know how to change/access that variable. It says obj.e1 is undefined. If I do obj['fill'] it still doesn't work.

如何从这样的循环(或类似方式)更改颜色.

How would I change the colors from such a loop (or something similar).

推荐答案

不是e1,而是el(小写的"L").而且您仍然想使用attr()函数,例如:

It's not e1, it's el (lowercase 'L'). And you'll still want to use the attr() function, e.g.:

for (var i =0; i < booths.length; i++){
  var obj = booths[i]
  obj.el.attr('fill', obj['color']);
  obj.el.attr('checked', 'false');
  $("#"+obj.name).remove();
}

示例: http://codepen.io/paulroub/pen/yFwCq

这篇关于遍历数组并设置变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆