我获取了 li
元素的 id
,并希望使用该名称来访问对象的该部分。我尝试添加 var SelectionId
,其中包含我想要的部分的名称,例如 cars
但出现错误。有办法吗?
谢谢。
$(document).ready(function() {
$(".markerSelection").click(function() {
var selectionId = $(this).attr("id");
drop(selectionId);
});
});
var markers = {
shopping : [
new google.maps.LatLng(52.26183, -7.11339),
new google.maps.LatLng(52.26134, -7.11226),
new google.maps.LatLng(52.26067, -7.11181),
new google.maps.LatLng(52.26003, -7.11033)],
cars : [
new google.maps.LatLng(52.26183, -7.11339),
new google.maps.LatLng(52.26134, -7.11226),
new google.maps.LatLng(52.26067, -7.11181),
new google.maps.LatLng(52.26003, -7.11033)]
};
var iterator = 0;
function drop(selectionId) {
clearOverlays();
for (var i = 0; i < markers.selectionId.length; i++) {
setTimeout(function() {
addMarker();
}, i * 200);
}
}
最佳答案
如果您尝试通过变量键访问对象属性,则需要使用数组表示法:
... markers[selectionId].length ...
markers.selectionId
失败,因为它相当于 markers["selectionId"]
。
关于javascript - 将字符串传递给对象 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11487734/