2

我需要制作一个已完成的 Javascript 待办事项列表(清单),但我无法弄清楚如何在输入的每个项目中也显示编辑和删除按钮。这是我到目前为止所拥有的

<!doctype html>
<html>
<head>
  <title>To Do List</title>
  <link rel="stylesheet" type="text/css" href="ToDoList.css">
</head>
<body>
  <h1> To Do List</h1>
  <div id = "listBox">
    <input type="text" id="inItemText"><button id = "btnAdd">Add</button>
  </div>
  <div class="tasks-parent">
    <h4>Tasks:</h4>
    <ul id = "todolist">
    </ul>
  </div>
  <script src ="ToDoList.js"></script>
</body>
</html>
#btnAdd {
  text-transform: uppercase;
  background: #22B473;
  border: none;
  border-radius: 3px;
  font-weight: bold;
  color: #FFF;
  padding: 3px 10px;
  cursor: pointer;
  width: auto;
}

.tasks-parent {
  border: 2px solid #777;
  margin-top: 5px;
  width: 17%;
}

html {
  font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
}

ul {
  list-style: none;
  padding: 0;
  margin: 0;
  width: 400px;
}

li {
  padding: 5px 10px;
  color: #000;
}

li span {
  padding-left: 17px;
}
function updateItemStatus() {
  var cbId = this.id.replace("cb_", "");
  var itemText = document.getElementById("item_" + cbId);
  if (this.checked) {
    itemText.style.textDecoration = "line-through";
  } else {
    itemText.style.textDecoration = "none";
  }
}

function addNewItem(list, itemText) {
  var date = new Date();
  var id = "" + date.getMinutes(); + date.getSeconds() +
    date.getMilliseconds() + "";

  var listItem = document.createElement("li");
  listItem.id = "li_" + id;

  var checkBox = document.createElement("input");
  checkBox.type = "checkbox";
  checkBox.id = "cb_" + id;
  checkBox.onclick = updateItemStatus;

  var span = document.createElement("span");
  span.id = "item_" + id;
  span.innerText = itemText;
  listItem.appendChild(checkBox);
  listItem.appendChild(span);
  list.appendChild(listItem);
}

var inItemText = document.getElementById("inItemText");
inItemText.focus();

var btnNew = document.getElementById("btnAdd");
btnNew.onclick = function() {
  var inItemText = document.getElementById("inItemText");
  var itemText = inItemText.value;
  if (!itemText || itemText === "" || itemText === " ") {
    return false;
  }

  addNewItem(document.getElementById("todolist"), itemText);
};

inItemText.onkeyup = function(event) {
  if (event.which == 13) {
    var itemText = inItemText.value;

    if (!itemText || itemText === "" || itemText === " ") {
      return false;
    }

    addNewItem(document.getElementById("todolist"), itemText);
    inItemText.focus();
    inItemText.select();
  }
};
4

2 回答 2

0

您需要创建另外两个元素(一个用于编辑,一个用于删除)并像处理其他元素一样附加它们:

function updateItemStatus() {
  var cbId = this.id.replace("cb_", "");
  var itemText = document.getElementById("item_" + cbId);

  if (this.checked) {
    itemText.style.textDecoration = "line-through";
  } else {
    itemText.style.textDecoration = "none";
  }

}


function addNewItem(list, itemText) {

  var date = new Date();
  var id = "" + date.getMinutes(); + date.getSeconds() +
    date.getMilliseconds() + "";

  var listItem = document.createElement("li");
  listItem.id = "li_" + id;

  var checkBox = document.createElement("input");
  checkBox.type = "checkbox";
  checkBox.id = "cb_" + id;
  checkBox.onclick = updateItemStatus;

  var span = document.createElement("span");
  span.id = "item_" + id;
  span.innerText = itemText;
  //create edit
  var edit = document.createElement("a");
  edit.href = "#";
  edit.innerText = "edit";
  //create delete
  var deleteBtn = document.createElement("a");
  deleteBtn.href = "#";
  deleteBtn.innerText = "delete";


  listItem.appendChild(checkBox);
  listItem.appendChild(span);
  //and append them in li
  listItem.appendChild(edit);
  listItem.appendChild(deleteBtn);

  list.appendChild(listItem);
}

var inItemText = document.getElementById("inItemText");
inItemText.focus();

var btnNew = document.getElementById("btnAdd");
btnNew.onclick = function() {
  var inItemText = document.getElementById("inItemText");

  var itemText = inItemText.value;
  if (!itemText || itemText === "" || itemText === " ") {
    return false;
  }

  addNewItem(document.getElementById("todolist"), itemText);
};

inItemText.onkeyup = function(event) {

  if (event.which == 13) {
    var itemText = inItemText.value;

    if (!itemText || itemText === "" || itemText === " ") {
      return false;
    }

    addNewItem(document.getElementById("todolist"), itemText);

    inItemText.focus();
    inItemText.select();
  }

};
#btnAdd {
  text-transform: uppercase;
  background: #22B473;
  border: none;
  border-radius: 3px;
  font-weight: bold;
  color: #FFF;
  padding: 3px 10px;
  cursor: pointer;
  width: auto;
}
.tasks-parent {
  border: 2px solid #777;
  margin-top: 5px;
  width: 35%;
}
html {
  font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
}
ul {
  list-style: none;
  padding: 0;
  margin: 0;
  width: 400px;
}
li {
  padding: 5px 10px;
  color: #000;
}
li span {
  padding-left: 17px;
}
li a {
  padding: 0 5px 0 5px;
}
<h1> To Do List</h1>
<div id="listBox">
  <input type="text" id="inItemText">
  <button id="btnAdd">Add</button>
</div>
<div class="tasks-parent">
  <h4>Tasks:</h4>
  <ul id="todolist">
  </ul>
</div>

于 2016-03-17T20:03:56.623 回答
0

您需要在附加的范围内包含按钮。还需要将 .innerText 更改为 .innerHTML

span.innerHTML = itemText + "<button>Edit</button><button>Delete</button>";

向插入的按钮添加类以设置样式

于 2016-03-17T20:27:02.070 回答