Javascript jQuery中$each的奇怪行为
我正在编写一段代码,从MySQL数据库获取SVG路径,并使用raphaeljs.com的脚本绘制形状。我在onmouseover属性上遇到了问题:我希望在悬停每个形状时获得不同的填充颜色,但发生的情况是,每当我悬停任何形状时,最后绘制的形状都是彩色的,而不是我悬停的形状 下面是JS函数的代码,用于绘制数据中包含的形状:Javascript jQuery中$each的奇怪行为,javascript,jquery,onmouseover,Javascript,Jquery,Onmouseover,我正在编写一段代码,从MySQL数据库获取SVG路径,并使用raphaeljs.com的脚本绘制形状。我在onmouseover属性上遇到了问题:我希望在悬停每个形状时获得不同的填充颜色,但发生的情况是,每当我悬停任何形状时,最后绘制的形状都是彩色的,而不是我悬停的形状 下面是JS函数的代码,用于绘制数据中包含的形状: function drawShapes(data,geolevel,transparent){ $.each(data, function(code,shape){
function drawShapes(data,geolevel,transparent){
$.each(data, function(code,shape){
var contour = shape.contour.split(" ");
attributes = {};
attributes["fill"] = (transparent ? "none" : shape.fillcolor);
attributes["fill-opacity"] = "0.75";
attributes["stroke"] = shapeProperties[geolevel]["stroke"];
attributes["stroke-width"] = shapeProperties[geolevel]["stroke-width"];
index = shapeProperties[geolevel]["prefix"] + code;
shapes[index] = drawPath("M " + contour.join(" ") + " z").attr(attributes);
shapes[index].fill = shape.fillcolor;
if (!transparent) {
shapes[index][0].onmouseover = function () {
shapes[index].attr({fill: hoverfill});
};
shapes[index][0].onmouseout = function () {
shapes[index].attr({fill: shapes[index].fill});
};
}
});
}
shapeProperties
是一个全局变量(对象),包含形状的属性,具体取决于形状的类型
我的房子周围有什么问题吗?
有关信息,我的脚本大致基于此演示:
提前谢谢 这一行:
index = shapeProperties[geolevel]["prefix"] + code;
看起来它在声明一个全局变量,这可能是问题的原因。使用var
关键字,以便将其范围限定到函数