【HTML+CSS+JavaScript】實現待辦事項(純DOM實現)

來源:互聯網
上載者:User

標籤:cto   poi   int   pen   lis   nod   ===   back   pre   

需求:實現待辦事項

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>todoList</title>    <style>            #todoList {            list-style: none;            margin:10px 0px;            padding:0;            width:600px;        }        #todoList li {            margin-bottom:5px;            padding: 10px;            border: 1px solid #ccc;            background:#f5f5f5;            position: relative;        }        input {            padding:10px;            font-size:16px;            border:1px solid #ccc;        }        button {            padding:10px 20px;            border:1px solid #ccc;            background: #f5f5f5;            outline: none;            cursor: pointer;        }        #todoList span {            position: absolute;            right: 10px;            cursor: pointer;        }    </style></head><body>    <input type="text" id="content">     <button id="btn">添加</button>    <ul id="todoList">        <li>取釣魚 <span>&times;</span></li>        <li>取洗澡 <span>&times;</span></li>        <li>取吃飯 <span>&times;</span></li>        <li>去睡覺 <span>&times;</span></li>    </ul>    <script>            var input = document.querySelector(‘#content‘);        var btn = document.querySelector(‘#btn‘);        var todoList= document.querySelector(‘#todoList‘);        var spans = document.querySelectorAll(‘#todoList span‘);        btn.onclick = function(){            //擷取 input的內建            var text = input.value;            //建立li元素 並給li元素添加包裹 內容            var li = document.createElement(‘li‘);            li.innerText = text;            var span = document.createElement(‘span‘);            span.innerHTML = ‘&times;‘;            li.appendChild(span);            //把li元素添加到ul中            todoList.appendChild(li);        }            /*spans.forEach(function(span){            span.onclick = function(){                todoList.removeChild(this.parentNode)            }        })*/        //委派方式綁定        todoList.onclick = function(event) {            if (event.target.nodeName === ‘SPAN‘) {                this.removeChild(event.target.parentNode);            }        }    </script></body></html>

 

【HTML+CSS+JavaScript】實現待辦事項(純DOM實現)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.