向数组添加元素
- <!DOCTYPE html>
- <html>
- <body>
- <h1>JavaScript 数组</h1>
- <p>length 属性提供了一种在不使用 push() 方法的情况下将新元素附加到数组的简便方法。</p>
- <button onclick="myFunction()">试一试</button>
- <p id="demo"></p>
- <script>
- var fruits = ["Banana", "Orange", "Apple", "Mango"];
- document.getElementById("demo").innerHTML = fruits;
- function myFunction() {
- fruits[fruits.length] = "Lemon";
- document.getElementById("demo").innerHTML = fruits;
- }
- </script>
- </body>
- </html>