用這段代碼我試圖遍歷從json數組派生的數組。 頁面內有一個SVG。 當我點擊一個國家時,這個名字應該被提交給一個URL。 不幸的是我得到在該行下面的錯誤與指示 - >錯誤用於數組元素的addEventListener
遺漏的類型錯誤:無法調用空
的方法「的addEventListener」我怎樣才能克服這種錯誤?爲什麼會發生?
數組是這樣的:
{"Countries":["india","switzerland","usa","alaska","germany","austria","netherlands","france","italy","spain","poland","hungary","czech","romania","russia","china","mexico","brazil","britain","thailand","sweden","uae","new_zealand_north_island","new_zealand_south_island","egypt"]}
var mySVG = document.getElementById("VectorMap");
var svgDoc;
mySVG.addEventListener("load", function() {
svgDoc = mySVG.contentDocument;
$.getJSON("http://www.someurl.com/jsonArray",
function (data) {
$.each(data, function (index, item) {
var i=0;
for (tot=item.length; i < tot; i++) {
var someElement = svgDoc.getElementById(item[i]);
//--->error
someElement.addEventListener("mousedown", function() {
$("#info").html(ajax_load).load("http://www.someurl.com/returnData"+"?country="+text);
}, false); //add behaviour
}
});
});
}, false);