简体   繁体   English

我的所有代码都被最后一部分覆盖

[英]All my code is being overwritten by the last section

We have a map which uses polygons to create overlays on top of countries. 我们有一张地图,该地图使用多边形在国家/地区上方创建叠加层。 When a user hovers over a country the polygons change color. 当用户将鼠标悬停在一个国家上时,多边形会更改颜色。

When the mouse leave the country it changes back (or at least we want it to) 当鼠标离开该国家时,它变回原来的状态(或者至少我们希望它变回该状态)

Us the code below what happens is that that both countries access the settings for JUST the last section of code. 我们下面看到的代码是,两个国家都访问了代码的最后一部分的设置。 It seems all other code is overwritten. 似乎所有其他代码都被覆盖。

I don't know which variable to make unique. 我不知道哪个变量要唯一。

for(var i = 0; i < germany.length; i++){
    addListener( germany[ i ], germany );
    }
function addListener( germany_item, tweened )
{   
    google.maps.event.addListener(germany_item, "mouseover",function() {
                    for( var i in tweened ) 
        tweened[ i ].setOptions({ fillColor: "#DD732B", strokeColor: "#DD732B" });
    });
    google.maps.event.addListener(germany_item, "mouseout",function() {
                    for( var i in tweened ) 
        tweened[ i ].setOptions({ fillColor: "#5EA9BD", strokeColor: "#5EA9BD" });
    });
}//
for(var i = 0; i < france.length; i++){
    addListener( france[ i ], france );
    }
function addListener( france_item, tweened )
{   
    google.maps.event.addListener(france_item, "mouseover",function() {
                    for( var i in tweened ) 
        tweened[ i ].setOptions({ fillColor: "#DD732B", strokeColor: "#DD732B" });
        });
    google.maps.event.addListener(france_item, "mouseout",function() {    
                    for( var i in tweened ) 
            tweened[ i ].setOptions({ fillColor: "#006886", strokeColor: "#006886" });
        });

You cannot have two functions with the same name (you have two copies of function addListener() ). 您不能有两个具有相同名称的function addListener() (您具有function addListener()两个副本)。 If you do that, then only the last one will be active (which is what you were experiencing). 如果这样做,那么只有最后一个将处于活动状态(这就是您所经历的)。 I would suggest you either change the name of both functions to addListenerGermany() and addListenerFrance() or replace them both with one function and pass in the minor difference between the two as a parameter so you can serve both need with one block of code. 我建议您将两个函数的名称都更改为addListenerGermany()addListenerFrance()或者将它们都替换为一个函数,并将两者之间的细微差别作为参数传递,以便可以用一个代码块满足这两个需求。

For example, you could change it all to this: 例如,您可以将其全部更改为:

for (var i = 0; i < germany.length; i++) {
    addListenerCommon( germany[ i ], germany , "#5EA9BD", "#5EA9BD");
}

for(var i = 0; i < france.length; i++) {
    addListenerCommon( france[ i ], france, "#006886", "#006886" );
}

function addListenerCommon(item, tweened, fill, stroke ) {   
    google.maps.event.addListener(item, "mouseover",function() {
        for( var i in tweened ) {
            tweened[ i ].setOptions({ fillColor: "#DD732B", strokeColor: "#DD732B" });
        }
    });
    google.maps.event.addListener(item, "mouseout",function() {
        for( var i in tweened ) {
            tweened[ i ].setOptions({ fillColor: fill, strokeColor: stroke });
        }
    });
}

If you're going to add more countries, then you can make a function for the for loop: 如果要添加更多国家/地区,则可以为for循环创建一个函数:

function initCountry(country, fill, stroke) {
    for (var i = 0; i < country.length; i++) {
        addListenerCommon(country[i], country, fill, stroke);
    }
}

initCountry(germany, "#5EA9BD", "#5EA9BD");
initCountry(france, "#006886", "#006886");
initCountry(australia, "#FF6886", "#FF6886");

and so on... 等等...

See this related question . 请参阅此相关问题 Using the function myFunction() syntax defines a function at parse time, not at run time, so the last declaration will overwrite the first. 使用function myFunction()语法在解析时(而不是在运行时function myFunction()定义一个函数,因此最后一个声明将覆盖第一个声明。 You can fix this by using var myFunction = function() syntax, which is defined at runtime: 您可以使用var myFunction = function()语法var myFunction = function()在运行时定义var myFunction = function()来解决此问题:

var addListener = function( germany_item, tweened ) { ... };
addListener(germany);
var addListener = function( france_item, tweened ) { ... };
addListener(france);

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

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