樣式.css

 body {            font-family: Arial, sans-serif;            margin: 20px;            padding: 0;        }        #container {            max-width: 500px;            margin: 0 auto;            padding: 20px;            border: 1px solid #ddd;            border-radius: 5px;        }        h2 {            text-align: center;        }        input[type=\\\"text\\\"] {            width: calc(100% - 24px);            padding: 10px;            margin-bottom: 10px;            border: 1px solid #ccc;            border-radius: 3px;        }        button {            width: 100%;            padding: 10px;            background-color: #28a745;            color: #fff;            border: none;            border-radius: 3px;            cursor: pointer;        }        button:hover {            background-color: #218838;        }        h4 {            margin-top: 20px;            color: #555;        }

\\\"Image

","image":"http://www.luping.net","datePublished":"2024-07-31T16:24:03+08:00","dateModified":"2024-07-31T16:24:03+08:00","author":{"@type":"Person","name":"luping.net","url":"https://www.luping.net/articlelist/0_1.html"}}
」工欲善其事,必先利其器。「—孔子《論語.錄靈公》
首頁 > 程式設計 > JavaScript 中的 push() 方法

JavaScript 中的 push() 方法

發佈於2024-07-31
瀏覽:507

JavaScript 中的 push() 方法將一個或多個元素加入到陣列末尾。此方法修改原始數組並傳回數組的新長度。

句法 :

array.push(element1, element2, ..., elementN);

*範例 1.: *

const fruits = ["Apple", "Banana"];
fruits.push("Orange", "Mango");
console.log(fruits); // Output: ["Apple", "Banana", "Orange", "Mango"]

*範例 2.: *
如何使用push()方法動態新增元素

index.html



    
    
    Fruit List
   


    

Fruit List

樣式.css

 body {
            font-family: Arial, sans-serif;
            margin: 20px;
            padding: 0;
        }
        #container {
            max-width: 500px;
            margin: 0 auto;
            padding: 20px;
            border: 1px solid #ddd;
            border-radius: 5px;
        }
        h2 {
            text-align: center;
        }
        input[type="text"] {
            width: calc(100% - 24px);
            padding: 10px;
            margin-bottom: 10px;
            border: 1px solid #ccc;
            border-radius: 3px;
        }
        button {
            width: 100%;
            padding: 10px;
            background-color: #28a745;
            color: #fff;
            border: none;
            border-radius: 3px;
            cursor: pointer;
        }
        button:hover {
            background-color: #218838;
        }
        h4 {
            margin-top: 20px;
            color: #555;
        }

Image description

版本聲明 本文轉載於:https://dev.to/sudhanshu_developer/explanation-of-push-method-in-javascript-3cb5?1如有侵犯,請聯絡[email protected]刪除
最新教學 更多>

免責聲明: 提供的所有資源部分來自互聯網,如果有侵犯您的版權或其他權益,請說明詳細緣由並提供版權或權益證明然後發到郵箱:[email protected] 我們會在第一時間內為您處理。

Copyright© 2022 湘ICP备2022001581号-3