我有一个简单的脚本,它从一个数组创建几个 Raphael 对象,然后为每个对象分配事件处理程序。问题是只有最后一个事件处理程序正在为所有对象执行。我在这里做错了什么?
var blockDiagram = {
"block" :[
{
"width": 100,
"height": 100,
"text" : "this is block 1",
"cx": 10,
"cy": 10,
"fill" : "blue"
},
{
"width": 100,
"height": 100,
"text" : "this is block 2",
"cx": 120,
"cy": 10,
"fill" : "yellow"
},
{
"width": 100,
"height": 100,
"text" : "this is block 3",
"cx": 230,
"cy": 10,
"fill" : "red"
}
]
};
var paper = new Raphael("holder", 700, 700)
for ( i=0; i< blockDiagram.block.length; i++)
{
ms = 500;
width = blockDiagram.block[i].width;
height = blockDiagram.block[i].height;
text = blockDiagram.block[i].text;
cx = blockDiagram.block[i].cx;
cy = blockDiagram.block[i].cy;
fill = blockDiagram.block[i].fill;
p = paper.rect(cx,cy, width, height).attr({"fill": fill});
txt = paper.text(cx ,cy, text).attr({fill: 'black', stroke: "none", opacity: 0, "font-size": 15});
p.mouseover(function () {
txt.stop().animate({opacity: 1}, ms);
}).mouseout(function () {
txt.stop().animate({opacity: 0}, ms);
});
}