簡體   English   中英

使用JS和jQuery推送到數組

[英]push to array with JS and jQuery

你能告訴我如何將文本字段中的文本添加到數組中,以便它顯示在文本字段的頂部嗎? 謝謝

<div id="test">
    <p id="items"></p>
    <input type="text" id="color" />
    <input type="button" id="add" value="Add" />
</div>
<script>
    var theArray = new Array("Red", "Green", "Blue");
    var target = $("#items");
    for (var i = 0; i < theArray.length; i++) {
        target.append("<p>" + theArray[i] + "</p>");
    }
    $("#add").click(function () {});
</script>

采用

$("#add").click(function () {
    var color = $("#color").val()
    theArray.push(color);
    target.prepend("<p>" + color + "</p>");
});

DEMO

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM