本文實例講述了JavaScript使用slice函數獲取數組部分元素的方法。分享給大家供大家參考。具體如下:
JS數組帶有一個slice方法,可以獲取數組的指定部分,下面的代碼獲取數組中的第二個和第三個元素
<!DOCTYPE html><html><body><p id="demo">Click the button to extract the second and the third elements from the array.</p><button onclick="myFunction()">Try it</button><script>function myFunction(){var fruits = ["Banana","Orange","Lemon","Apple","Mango"];var citrus = fruits.slice(1,3);var x=document.getElementById("demo");x.innerHTML=citrus;}</script></body></html>上面的代碼輸出
Orange,Lemon
希望本文所述對大家的javascript程序設計有所幫助。
新聞熱點
疑難解答