1

我有以下对象数据:

var js_images = {
"newborn_set01_01.jpg":{"filename":"newborn_set01_01.jpg","title":"First Pic","description":"First description..."},
"newborn_set01_02.jpg":{"filename":"newborn_set01_02.jpg","title":"Second Pic","description":"Second description"},
"newborn_set01_03.jpg":{"filename":"newborn_set01_03.jpg","title":"Third Pic","description":"Third description"}
};

我将如何做一些简单的事情,比如创建一个警报来显示描述newborn_set01_03.jpg

这显然不起作用:

alert(js_images.newborn_set01_03.jpg.description);
4

1 回答 1

4

除了点表示法之外,您还可以使用方括号表示法访问对象成员:

alert(js_images["newborn_set01_03.jpg"].description);

文档:MDN - 会员运营商

于 2012-12-04T02:05:22.413 回答