我在jquery中有一个数组,它包含问题答案的子集,但在之前 用户进入q& a我想显示用户记忆的答案。有人可以解释一下 我可以遍历数组并依次提取每个图像?
var imageAnswers = {"q1toQn": [
{"q1": [
{"src": "./images/q1/cow", "alt": "cow", "id": "cow"},
{"src": "./images/q1/horse", "alt": "horse", "id": "horse"},
{"src": "./images/q1/pig", "alt": "pig", "id": "pig"},
{"src": "./images/q1/sheep", "alt": "sheep", "id": "sheep"}
]},
{"q2": [
{"src": "./images/q2/apple_", "alt": "apple", "id": "apple"},
{"src": "./images/q2/pear_", "alt": "pear", "id": "pear"},
{"src": "./images/q2/orange_", "alt": "orange", "id": "orange"},
{"src": "./images/q2/pineapple_", "alt": "pinaapple", "id": "pineapple"}
]},
{"q3": [
{"src": "./images/q3/chair", "alt": "chair", "id": "chair"},
{"src": "./images/q3/bookcase", "alt": "bookcase", "id": "bookcase"},
{"src": "./images/q3/chest", "alt": "chest", "id": "chest"},
{"src": "./images/q3/table", "alt": "table", "id": "table"}
]}, ...
等等
以下是我正在尝试获取信息的内容:
var infoTableIndex = 0;
j(function(){
j("#nextButton").click(
function(){
j("#slideshow").not(":animated").animate({"left": "500px", "opacity": "0.3"}, 1000,
function(i){
infoTableIndex++;
j("#slideshow").attr("src", imageAnswers.q1toQn[infoTableIndex].q1[i+1].src + ".png");
j("#slideshow").css("left", "-500px");
j("#slideshow").animate({"left": "40px", "opacity": "0.1"}, 500);
})
}//no sc
);
});
我得到的错误是:
TypeError:表达式的结果 'imageAnswers.q1toQn [infoTableIndex]' [undefined]不是对象。
提前感谢您的帮助。
答案 0 :(得分:1)
.animate()
的“完整”回调函数不会发送任何索引或任何类型的参数,因此您的i
未定义。
如果它必须是一个连续的参数,你可以使用相同的infoTableIndex
var,所以你有:
imageAnswers.q1toQn[infoTableIndex].q1[infoTableIndex+1].src
希望这会有所帮助。干杯
PS:如果你更好地解释幻灯片的逻辑并给出一个例子
,那就太好了