javascript - 拉斐尔.js : [Onclick] Change color of the clicked element

标签 javascript html raphael

我在 Raphael.js 中构建了一张美国 map 。现在我希望每次单击某个状态时,它都会以循环方式改变颜色。 像这样:

White State --> Click --> Blue State --> Click --> Red --> Click --> White --> Click --> Blue and so on.

以下代码仅运行一次。就像当状态的填充颜色设置为蓝色时,单击时颜色会更改为红色,但在下一次单击时,它只会保持红色,不会更改为白色。有人有解决办法吗?

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');}
});
}

关于javascript - 拉斐尔.js : [Onclick] Change color of the clicked element,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31985334/

相关文章:

javascript - R2D3 的 QUnit 测试开箱即用失败 - 我做错了什么?

javascript - PHP 的替代品包括?

javascript - WordPress 下拉导航的 Z-Index 问题

javascript - HTML5/CSS3 - 内部带有谷歌地图的自定义形状 Div

javascript - jQuery div悬停幻灯片效果

php - 表单到 PHP,我需要将表单的内容发送到我的 php 并显示在新网页上的 div 样式中

javascript - 不透明度之间的延迟 : 0 and starting animation to opacity: 1 in Raphael JS

javascript - Raphael svg 反转 y 轴坐标

javascript - 未捕获的类型错误 : MyView is not a function

javascript - @section 脚本与 ASP.Net WebForm 中使用的常用脚本标记之间的区别