2011-09-11 35 views
0

我发现了一个未捕获的ReferenceError每当“countryDetail”功能,使用下面的代码调用“国家”变量:jQuery的变量

function load_countries(e) { 
var count = e.features.length;  
if (!count) {return;} 
for (var i = 0; i < count; i++) { 
    var feature = e.features[i]; 
    // get the country name & rank 
    country = feature.data.properties.NAME 
    score = rsfpfi2010[country] 

    feature.element.setAttribute("onmouseover", "countryDetail("+country+","+score+");"); 
     feature.element.setAttribute("onmouseout", "hideCountryDetail();"); 
    feature.element.onmousemove = detailFollow; 
} 
} 

// Tooltip content & display 
function countryDetail(c,s) { 
//console.log(c,s); 
if (score == undefined) { 
    $("#tooltip").html("<h3>"+ c +"</h3><br/>Data Unavailable"); 
} else { 
    $("#tooltip").html("<h3>"+ c +"</h3><br/>Score: " + s); 
} 
$("#tooltip").show(); 
} 

我不明白的是,如果我替换“国家'与'分数',如下面的行:

feature.element.setAttribute("onmouseover", "countryDetail("+score+","+score+");"); 

一切运行良好。 (当然,除了我没有得到我需要的数据)。

'score'变量是从头文件中加载的json文件中调用的,而'country'变量是直接从写入页面的geoJSON数据中提取的。

我几乎是积极的,我已经错过了一件简单的事情,但现在我完全与它结合。

在此先感谢! 弥敦道

+1

您是否尝试过报警'feature.data.properties.NAME',以确保它包含了你所认为的呢?显而易见的答案是,你的情况有误,或者有什么意思,确切的变量名不存在,因此未定义。 – Joe

+0

除了@乔的建议之外,别忘记忘记分号的坏习惯。 JavaScript是一种遗忘语言,但不要滥用它。另外,在'countryDetail'函数中,您正在检查'score == undefined',但变量'score'不存在于该函数的范围内,它肯定会抱怨它。 – Shef

+1

如果你开始使用jQuery - 无处不在:) – Samich

回答

0

尝试:的

$(feature.element).hover(function() { 
    countryDetail(country, score); 
}, function() { 
    hideCountryDetail(); 
}).move(detailFollow); 

代替

feature.element.setAttribute("onmouseover", "countryDetail("+country+","+score+");"); 
    feature.element.setAttribute("onmouseout", "hideCountryDetail();"); 
feature.element.onmousemove = detailFollow; 
+0

感谢您的建议。我通过将数据作为属性添加到svg路径然后使用@Samich建议的JQuery来实现它。不知道这是否是最好的方式,但它现在正在运行。如所建议的那样,还清理了Jquery。下面的代码以防万一有人需要它 – Nathan

+0

对不起,代码不适合... – Nathan