JS-待辦事項管理-2

從 DOM 樹移除元素、動態加上 onclick 事件

by igorrrrr

HTML

<div class="app">
      <div class="container">
        <h1>Todo List</h1>
        <div class="todoWrap">
          <div class="input-wrap">
            <input type="text" id="input" placeholder="請輸入待辦事項..." />
            <button class="add-btn" onclick="addList()">+</button>
          </div>
        </div>
        <div class="container">
          <ul id="list">
            <li><span>ex代辦事項:買外套</span><button>刪除</button></li>
          </ul>
        </div>
      </div>
    </div>

CSS

* {
        padding: 0;
        margin: 0;
      }
      ul li {
        list-style: none;
        text-align: left;
      }
      body {
        background-color: #bfc2e3;
      }
      .app {
        font-family: "Segoe UI", "Open Sans", "Helvetica Neue", sans-serif;
        width: 100%;
        /* height: 100vh; */
      }
      .container {
        margin: 0 auto;
        width: 400px;
        text-align: center;
      }
      .add-btn {
        background-color: #f0ecfc;
        background-image: linear-gradient(315deg, #f0ecfc 0%, #c797eb 74%);
        color: #fff;
        overflow: hidden;
        line-height: 18px;
        width: 25px;
        height: 25px;
        padding: 0;
        border: none;
        border-radius: 5px;
      }
      .add-btn::after {
        position: absolute;
        content: "";
        right: 0;
        bottom: 0;
        background: #c797eb;
      }
      .add-btn:hover {
        box-shadow: 4px 4px 6px 0 rgba(255, 255, 255, 0.5),
          -4px -4px 6px 0 rgba(116, 125, 136, 0.5),
          inset 4px 4px 6px 0 rgba(255, 255, 255, 0.2),
          inset 4px 4px 6px 0 rgba(0, 0, 0, 0.4);
      }
      .todoWrap #input {
        height: 18px;
        border-radius: 80px;
        outline: 0;
        border: 1px solid rgb(255, 255, 255, 0.4);
        caret-color: transparent;
        background-color: transparent;
        padding: 5px 5px 5px 8px;
        font-size: 18px;
      }
      .todoWrap #input:focus {
        border: 1px rgb(255, 255, 255, 0.4) solid;
        box-shadow: 1px 2px 1px rgb(255, 255, 255, 0.4);
      }

      /* 設定代辦清單的樣式 */
      .container #list {
        display: flex;
        justify-content: center;
        flex-wrap: wrap;
      }
      .container #list li {
        display: flex;
        justify-content: center;
      }
      .container #list li span {
        display: inline-block;
        background-color: #f0ecfc;
        height: 20px;
        line-height: 20px;
        font-size: 18px;
       ...

JavaScript

var input = document.querySelector("#input");
      //將Todo的資料顯示在下方
      var list = document.getElementById("list");
      function addList() {
        var todoContent = document.createElement("li");
        var todoSpan = document.createElement("span");
        var butDel = document.createElement("button"); // 按下+,刪除鍵會一併產出
        var inputValue = input.value;
        todoSpan.textContent = inputValue;
        list.append(todoContent);
        todoContent.append(todoSpan);
        console.log(todoSpan);
        todoContent.append(butDel);
        butDel.textContent = "刪除";
        butDel.onclick = deleteli;
      }
      function deleteli() {
        event.target.parentElement.remove();
      }