HTML:
<h3>原始數組</h3> <div id="show5"></div> <h3>原數組的一部分</h3> <div id="show6"></div> <h3>原數組的剩下一部分</h3> <div id="show7"></div>
Jquery:
var animals = ['dog','cat','tiger','pig','bird']; $('#show5').html(animals.join('<br/>')); //拆分數組 animalsOfPart = animals.splice(0, 3); //一部分 $('#show6').html(animalsOfPart.join('<br/>')); //另一部分剩余在原數組中 $('#show7').html(animals.join('<br/>'));
顯示結果:
原始數組
dog
cat
tiger
pig
bird
原數組的一部分
dog
cat
tiger
原數組的剩下一部分
pig
bird
知識點:
1,splice(0,3)表示從索引0開始,從原數組里刪除3個元素。從原來的數組中提取並返回這兩個參數所定義范圍內的數組元素,並保存到另外一個數組里。總之,刪除並返回數組中的一部分(由splice的兩個參數所定義),剩余部分留在原數組中。