2015-08-13 39 views
1

我已经在Raphael.js中构建了美国地图。现在我希望每次点击某个状态时,它都会以循环的种类改变颜色。 像这样:Raphael.js:[Onclick]更改点击元素的颜色

白状态 - >点击 - >蓝邦 - >点击 - >红色 - >点击 - >白 - >点击 - >蓝等。

以下代码仅工作一次。就像状态的填充颜色设置为蓝色时一样,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');} 
}); 
} 

谢谢!

+0

认为你可能需要包括多一点的代码可以肯定,理想上的jsfiddle。另外请注意,您不需要使用'this.node.setAttribute',只需使用'this.attr('就像您在检查时一样。 – Ian

+0

谢谢!只需替换'.node'即可使用。我给你绿色的钩子?很抱歉,新的在这里... – Ilija

回答

1

只是为了记录在案,这工作(感谢伊恩):

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