<!DOCTYPE html>
<html lang="zh-Hant-TW">
<head>
    <title>Learing Javascript</title>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1" />

    <style type="text/css">
        
    </style>

</head>

<body>

    <input id="myInput" type="text" name="" value="my Value">

    <button id="myButton">請點按</button>

    <p id="text">原來的一段文字</p>

    <script type="text/javascript">

        var myArray = ["pizza","chocolate"];

        myArray.push("apple");  //新增陣列元素
        console.log(myArray);

        myArray.splice(1,1);    //減少陣列元素 從第1個開始,少1個元素
        console.log(myArray);

        myArray.splice(1,2);    //減少陣列元素 從第1個開始,少2個元素
        console.log(myArray);

        myArray.splice(0,0,"orange");   //利用splice新增元素,在第0個前面,減少0個元素,加上orange元素
        console.log(myArray);
        

        document.getElementById("myButton").onclick = function(){

            document.getElementById("text").innerHTML = myArray[0];

        }
    
    </script>


</body>
</html>


 

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 A&B 的頭像
    A&B

    網頁前端新手上路辛酸史

    A&B 發表在 痞客邦 留言(0) 人氣()