我在下面有這個簡單的代碼。我試圖查詢在輸入框中輸入的任何形狀的名稱。我可以正確地傳遞函數中的參數,但是當我嘗試使用參數查詢它時,它會導致我出錯。下面如何將參數傳遞給變量來訪問對象
<div id="page">
<input type="text" id="getShape"/>
<input type="button" onClick="getShapeDetails()" />
</div>
<script>
shapes = {
"circle" : {"name":"circle", "sides":"0", "color":"yellow"},
"triangle" : {"name":"triangle", "sides":"3", "color":"red"},
"square" : {"name":"square", "sides":"4", "color":"blue"},
};
function getShapeDetails(){
var getShape = document.getElementById("getShape").value;
function getShapesDetail(shape){
console.log(shape);
console.log(shapes.shape.name);
}
getShapesDetail(getShape);
}
</script>
,您發佈的代碼無關使用JSON。 JSON是*數據交換格式*,如XML或CSV。在你的例子中,'shapes'意味着一個JavaScript對象。 – 2013-04-07 16:50:03