Javascript-Dom-添加

<body>
        <input type="button" name="" value="Create" onclick="add()" />
        <hr width="245px" size="1" align="left" />
        <input type="text" name="" value="" id="txt" />
        <ol id="oList">
            <li>Black metal</li>
            <li>Alternative metal</li>
            <li>Thrash metal</li>
            <li>Doom metal</li>
            <li>Classical metal</li>
            <li>Gothic metal</li>
        </ol>
    </body>
<script type="text/javascript">
        function add(){
            var oList = document.getElementById("oList");
            var listItems = document.createElement("li");
            //listItems.innerHTML = txt.value;//这样写也能将文本框输入的值赋予到添加的LI元素中
            listItems.innerHTML = document.getElementById("txt").value;
            oList.appendChild(listItems);
        }
    </script>

 

posted @ 2017-01-12 17:05  Western_Journalist  阅读(164)  评论(0编辑  收藏  举报