1 回答

TA貢獻(xiàn)1995條經(jīng)驗(yàn) 獲得超2個(gè)贊
問題是,當(dāng)您在偵聽器中添加一個(gè)函數(shù)并將其添加到該函數(shù)時(shí),()它會(huì)直接執(zhí)行,而不是在執(zhí)行偵聽器時(shí)執(zhí)行。
所以正確的是:_newMarker.addEventListener('click', markerDetails)
將您的代碼更改為:
for (obj in Allpoints){
_newMarker = L.marker(Allpoints[obj].LatLong,
{title: Allpoints[obj].names,
riseOnHover: true,
},
).addTo(mymap);
_newMarker.data = Allpoints[obj]; // save the objectData on the marker
allMarkers.push(_newMarker);
_newMarker.on('click', markerDetails);
}
function markerDetails(e){
var currentMarker = e.target;
var obj = currentMarker.data; //your objectData
$("#returnControlName").html(currentMarker.controlName);
$("#returnControlEastings").html(controlEastings);
$("#returnControlNorthings").html(controlNorthings);
}
添加回答
舉報(bào)