向数组添加元素

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. <h1>JavaScript 数组方法</h1>
  6.  
  7. <h2>push()</h2>
  8.  
  9. <p>push() 方法将新元素追加到数组中。</p>
  10.  
  11. <button onclick="myFunction()">试一试</button>
  12.  
  13. <p id="demo"></p>
  14.  
  15. <script>
  16. var fruits = ["Banana", "Orange", "Apple", "Mango"];
  17. document.getElementById("demo").innerHTML = fruits;
  18.  
  19. function myFunction() {
  20. fruits.push("Kiwi");
  21. document.getElementById("demo").innerHTML = fruits;
  22. }
  23. </script>
  24.  
  25. </body>
  26. </html>