源代码
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>享学课堂</title> </head> <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>
运行结果