jQuery 是用 Javascript 编写的,Javascript 本身提供了 Array 对象。
所以访问数组的第 0 个元素是array_name[0]
在您的示例中,您将对象存储为数组的元素。您的对象包含一个“索引”属性,但请注意,您的“索引”属性与数组中的元素索引无关。您不应该包含“索引”属性...例如:
var lotsData = [
{ // this is the first array element, index value in the array is 0
index: 1,
data: 'I want to be in HTML',
},
{ // this is the second array element, index value in the array is 1
index: 0,
data: "I don't want to be in HTML",
}]
lotsData[0].data // value: 'I want to be in HTML'
更好的例子是:
var lotsData = [
{ // this is the first array element, index value in the array is 0
category: 'shoe',
detail: 'red'
},
{ // this is the second array element, index value in the array is 1
category: 'fruit',
detail: 'apple'
}]
lotsData[0].detail // value: 'red'
添加:尾随逗号
请注意,虽然 Javascript 是一种功能强大的语言,但它也有其怪癖。
一个重要的是尾随逗号,例如
...
index: 0,
data: "I don't want to be in HTML", // Trailing comma. DON'T DO THIS!
}]
问题在于尾随逗号不是 Javascript 语言的正式部分。大多数 JS 引擎都支持它,但一个非常重要的引擎不支持:Internet Explorer 浏览器确实不支持尾随逗号,遇到逗号就会悲痛地死去。
由于 IE 的独特处理方式,您的测试应始终包括 IE。
我在 IE 7 中测试。