我发现了一个未捕获的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数据中提取的。
我几乎是积极的,我已经错过了一件简单的事情,但现在我完全与它结合。
在此先感谢! 弥敦道
您是否尝试过报警'feature.data.properties.NAME',以确保它包含了你所认为的呢?显而易见的答案是,你的情况有误,或者有什么意思,确切的变量名不存在,因此未定义。 – Joe
除了@乔的建议之外,别忘记忘记分号的坏习惯。 JavaScript是一种遗忘语言,但不要滥用它。另外,在'countryDetail'函数中,您正在检查'score == undefined',但变量'score'不存在于该函数的范围内,它肯定会抱怨它。 – Shef
如果你开始使用jQuery - 无处不在:) – Samich