2012-07-14 96 views
0

我得到li元素的id,并且想要使用该名称来访问对象的该部分。我试图添加包含我想要的部分的名称的var selectionId,例如cars,但出现错误。有没有办法?将字符串传递到对象javascript

谢谢。

$(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); 
    } 
} 

回答

4

如果您正在试图通过一个可变密钥来访问一个对象的属性,你需要使用数组表示法:

... markers[selectionId].length ... 

markers.selectionId失败,因为它相当于markers["selectionId"]

+0

什么downvote? -innocent halo- * [可能会或可能没有删除downvote由于曲解答案] * – 2012-07-14 22:18:28

+0

谢谢:D删除我的评论 – Hamish 2012-07-14 22:19:01

+0

不客气。实际上,+1是正确的。 – 2012-07-14 22:19:20

相关问题