繁体   English   中英

Raphael.js:[Onclick]更改被单击元素的颜色

[英]Raphael.js: [Onclick] Change color of the clicked element

我已经在Raphael.js中绘制了美国地图。 现在,我希望每次单击状态时,它都会像循环一样更改颜色。 像这样:

白色状态->单击->蓝色状态->单击->红色->单击->白色->单击->蓝色,依此类推。

以下代码仅工作一次。 就像状态的填充颜色设置为蓝色时一样,onclick的颜色变为红色,但是在下次单击时,颜色保持为红色,而不会变为白色。 有人有解决方案吗?

for(var i = 0; i< states.length; i++) {
 states[i].click(function() {
  if (this.attr('fill')=='white')
    {this.node.setAttribute('fill', 'blue');}
  else if (this.attr('fill')=='blue')
     {this.node.setAttribute('fill', 'red');}
  else {this.node.setAttribute('fill', 'white');}
});
}

谢谢!

仅作记录,这有效(感谢Ian):

for(var i = 0; i< states.length; i++) {
states[i].click(function() {
if (this.attr('fill')=='white')
  {this.attr('fill', 'blue');}
else if (this.attr('fill')=='blue')
  {this.attr('fill', 'red');}
else {this.attr('fill', 'white');}
});
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM