<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript">
testArray =[[ 'text1', 'text2', 'text3' ],
[ 'question',
'correct answer',
'second answer',
'third answer',
'fourth answer' ],
[ 'question',
'correct answer',
'second answer',
'third answer',
'fourth answer',
'fifth answer',
'sixth answer' ]];
</script>
</head>
<body>
<div id = "passage"></div>
<div id = "question"></div>
<div id = "answers"></div>
<script>
$.each(testArray[1].slice(1), function (i) {
$("#answers").append("<p>" + testArray[1][i] + "</p>");
});
</script>
</body>
</html>
当我跑了这一点,我是通过元素期待最后的每个循环来从testArray[1][1]
到testArray[1][4]
。不过由于某种原因,它只能达到testArray[1][3]
。有谁知道问题是什么?Jquery的每个循环没有得到最后一个元素
编辑:
我做到这一点,它以某种方式工作了什么,我想做的事(开始于指数1元,并循环到最后):
$.each(testArray[1].slice(1), function (i) {
$("#answers").append("<p>" + testArray[1][i + 1] + "</p>");
});
不知道如何/为什么这工作虽然。
'slice'删除最后一个元素。你不需要使用切片。只要删除它,它会起作用。 –
我想使用切片,以删除第一个元素(索引0),并从第二个元素(索引1)开始循环。 –
删除第一个元素,您需要使用'shift()'或'splice(0,1)' –