首页 > 解决方案 > JS todo list item选中,并添加item

问题描述

我的目标是创建一个类似于https://www.w3schools.com/howto/howto_js_todolist.asp的 JS 待办事项列表

我已经创建了我的待办事项列表应用程序的基本结构和关闭按钮的功能。这些运行良好,但我在添加新的待办事项列表项以及检查和取消选中待办事项时遇到问题。我不确定我是否很好地使用了类列表切换属性,也无法弄清楚为什么添加按钮根本不起作用。

var todoitemlist = document.getElementsByClassName('todo-item');

var i;
for (i = 0; i < todoitemlist.length; i++) {
  var span = document.createElement("SPAN");
  span.innerHTML = "Close";
  span.className = "closebutton";
  todoitemlist[i].appendChild(span);
}

var close = document.getElementsByClassName("closebutton");

var i;
for (i = 0; i < close.length; i++) {
  close[i].onclick = function() {
    var listitem = this.parentElement;
    listitem.style.display = "none";
  }
}
var todoitemlistx = document.getElementsByClassName('todo-item');
//checked element
var i;
for (i = 0; i < todoitemlistx.length; i++) {
  todoitemlistx[i].onclick = function(ev) {
    ev.style.backgroundColor = "red";
    ev.classList.toggle("todo-item-checked");
  }
}

//add another list item
function add() {
  var listitem = document.createElement("LI");
  listitem.className = "todo-item";
  var text = document.getElementById('todoinput').value;
  var myul = getElementById('todo-list');
  var t = document.createTextNode(text);
  listitem.appendChild(t);
  myul.appendChild(listitem);

  var span = document.createElement("SPAN");
  span.innerHTML = "Close";
  span.className = "closebutton";
  listitem[i].appendChild(span);

  for (i = 0; i < close.length; i++) {
    close[i].onclick = function() {
      var div = this.parentElement;
      div.style.display = "none";
    }
  }
}
* {
  box-sizing: border-box;
}

body {
  text-align: center;
  margin: 0;
  padding: 0;
  background-color: #dbf9fc;
  font-family: Arial, Helvetica, sans-serif;
  color: rgba(0, 27, 39);
}

#todoinput {
  margin: 5px;
  padding: 5px;
  width: 65%;
}

#add-button {
  padding: 5px;
  margin: 5px;
  width: 5%;
  background-color: rgba(0, 27, 39);
  color: #dbf9fc;
  border: none;
  border-radius: 5px;
  height: 1fr;
  cursor: pointer;
}

#add-button:hover {
  background-color: black;
}

#todo-list {
  display: inline-block;
  margin: 0;
  padding: 0;
  list-style: none;
  width: 70%;
}

.todo-item {
  position: relative;
  display: flex;
  justify-content: flex-start;
  background-color: white;
  border: 2px solid black;
  padding: 5px;
  margin: 5px;
}

.closebutton {
  cursor: pointer;
  justify-self: flex-end;
  background-color: #e6772d;
  position: absolute;
  right: 0;
  top: 0;
  color: white;
  float: right;
  padding: 5px;
  width: 30%;
  margin: 0;
}

.closebutton:hover {
  background-color: #c46526;
}

.todo-item-checked {
  position: relative;
  display: flex;
  justify-content: flex-start;
  background-color: rgb(187, 187, 187);
  border: 2px solid black;
  padding: 5px;
  margin: 5px;
  text-decoration: line-through;
}

.black {
  background-color: black;
}
<main class="centered">
  <h1>ToDo List JS</h1>
  <h3>js project</h3>
  <form action="">

    <input type="text" name="" id="todoinput" placeholder="Enter the activity you've wented to do">
    <input type="button" value="Add" id="add-button" onclick="add()">
  </form>
  <ul id="todo-list">
    <li class="todo-item">
      Hit the lights
    </li>
    <li class="todo-item">
      Hit the lights
    </li>
    <li class="todo-item">
      Hit the lights
    </li>
    <li class="todo-item-checked todo-item">
      Hit the lights
    </li>
  </ul>
</main>

标签: javascripthtmlcssfrontend

解决方案


我记得开始开发的感觉和难度,所以我坚持了下来。我认为这将是一些小的变化,但它变成了大规模的重写。

正如意大利人喜欢说的那样,Una grande padulo。

我希望你能或试着理解我在这里做了什么。

最大的教训是,不要两次编写相同的代码,将其放在单独的函数中。

所以这似乎有效。如果没有,请告诉我。

 var todoitemlist=document.getElementsByClassName('todo-item');

 for(var i=0;i<todoitemlist.length;i++)
 {
  myawesomeclosebutton(todoitemlist[i]);
  todoitemlist[i].addEventListener('click',myawesomebackground);
 }

 function myawesomeclosebutton(myawesomeitem)
 {
  var span=document.createElement('span');
  span.innerHTML='Close';
  span.className='closebutton';
  span.addEventListener('click',myawesomecloseevent);
  myawesomeitem.appendChild(span);
 }

 function myawesomebackground(ev)
 {
  if(ev.target.style.backgroundColor!='red')
  {
   ev.target.style.backgroundColor='red';
  }
  else
  {
   ev.target.style.backgroundColor='transparent';
  }
 }

 function myawesomecloseevent(event)
 {
  var div=event.target.parentElement;
  div.style.display='none';
 }

 function add()
 {
  var listitem=document.createElement('li');
  listitem.className='todo-item';
  var myawesomeinput=document.getElementById('todoinput');
  var text=myawesomeinput.value;
  var myul=document.getElementById('todo-list');
  var t=document.createTextNode(text);
  listitem.appendChild(t);
  myul.appendChild(listitem);
  listitem.addEventListener('click',myawesomebackground);
  myawesomeclosebutton(listitem);
  myawesomeinput.value='';
 }

推荐阅读