這篇文章主要介紹了JavaScript使用shift方法移除素組第一個元素的用法,實例分析了javascript中shift方法的使用技巧,需要的朋友可以參考下
本文實例講述了JavaScript使用shift方法移除素組第一個元素的用法。分享給大家供大家參考。具體如下:
JS中我們可以通過pop方法移除數組的最后一個元素,可以通過shift方法移除數組的第一個元素
- <!DOCTYPE html>
- <html>
- <body>
- <p id="demo">
- Click the button to remove the first
- element of the array.</p>
- <button onclick="myFunction()">Try it</button>
- <script>
- var fruits = ["Banana","Orange","Apple","Mango"];
- function myFunction()
- {
- fruits.shift();
- var x=document.getElementById("demo");
- x.innerHTML=fruits;
- }
- </script>
- </body>
- </html>
上面的代碼輸出如下結果
Orange,Apple,Mango
希望本文所述對大家的javascript程序設計有所幫助。
新聞熱點
疑難解答
圖片精選