Javascript 中待办事项列表的复选框符号

问题描述 投票:0回答:1

我正在尝试用 JavaScript 创建一个待办事项列表应用程序。我使用 HTML 和 CSS 创建了一个复选标记按钮。问题是添加新任务时不显示复选标记按钮,但任务已添加。有人可以向我解释一下出了什么问题以及应该如何做吗?

我不明白我的问题是来自 HTML、CSS 还是 JavaScript。

代码片段

let userinput = [];

function task() {
  const inputText = document.getElementById("inputbutton").value;

  // Check if inputText already exists in userinput array
  if (userinput.includes(inputText)) {
    alert("Already added");
    return; // Exit function early if inputText already exists
  }


  userinput.push(inputText);

  let tasklist = document.getElementById("tasklist");
  let listItem = document.createElement("li");
  listItem.textContent = inputText;
  tasklist.appendChild(listItem);

}

/*
I also tried this:
   let checkmarks = document.querySelectorAll('.container .checkmark');
    checkmarks.forEach(checkmark => {
        checkmark.style.display = 'block';
    });
*/
* {
  box-sizing: border-box;
  margin: 0;
}

#inputbutton {
  color: hsl(210deg 100% 44%);
  ;
  border: 2px solid hsl(210deg 100% 44%);
  ;
  border-radius: 10px;
  padding: 10px 25px;
  background: transparent;
  max-width: 190px;
  margin-top: 4em;
  margin-left: 42em;
  display: inline-flex;
  position: relative;
}

ul li {
  list-style: none;
  font-size: 17px;
  padding: 12px 8px 50px;
  user-select: none;
  cursor: pointer;
}

.input:active {
  box-shadow: 2px 2px 15px #8707ff inset;
}

button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  height: 40px;
  position: relative;
  padding: 0px 11px;
  font-size: 18px;
  text-transform: uppercase;
  border: 0;
  background-color: hsl(210deg 100% 44%);
  border-radius: 12px;
  overflow: hidden;
  transition: 31ms cubic-bezier(.5, .7, .4, 1);
}

button:before {
  content: attr(alt);
  display: flex;
  align-items: center;
  justify-content: center;
  position: absolute;
  inset: 0;
  font-size: 15px;
  font-weight: bold;
  color: white;
  letter-spacing: 4px;
  opacity: 1;
}

button:active {
  box-shadow: none;
  transform: translateY(7px);
  transition: 35ms cubic-bezier(.5, .7, .4, 1);
}

button:hover:before {
  transition: all .0s;
  transform: translateY(100%);
  opacity: 0;
}

button i {
  color: white;
  font-size: 15px;
  font-weight: bold;
  letter-spacing: 4px;
  font-style: normal;
  transition: all 2s ease;
  transform: translateY(-20px);
  opacity: 0;
}

button:hover i {
  transition: all .2s ease;
  transform: translateY(0px);
  opacity: 1;
}

button:hover i:nth-child(1) {
  transition-delay: 0.045s;
}

button:hover i:nth-child(2) {
  transition-delay: calc(0.045s * 3);
}

button:hover i:nth-child(3) {
  transition-delay: calc(0.045s * 4);
}

button:hover i:nth-child(4) {
  transition-delay: calc(0.045s * 5);
}

button:hover i:nth-child(6) {
  transition-delay: calc(0.045s * 6);
}

button:hover i:nth-child(7) {
  transition-delay: calc(0.045s * 7);
}

button:hover i:nth-child(8) {
  transition-delay: calc(0.045s * 8);
}

button:hover i:nth-child(9) {
  transition-delay: calc(0.045s * 9);
}

button:hover i:nth-child(10) {
  transition-delay: calc(0.045s * 10);
}


/* Hide the default checkbox */

.container input {
  display: none;
}

.container {
  display: block;
  position: relative;
  cursor: pointer;
  font-size: 20px;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}


/* Create a custom checkbox */

.checkmark {
  position: relative;
  top: 0;
  left: 0;
  height: 1.3em;
  width: 1.3em;
  background-color: #2196F300;
  border-radius: 0.25em;
  transition: all 0.25s;
}


/* When the checkbox is checked, add a blue background */

.container input:checked~.checkmark {
  background-color: #2196F3;
}


/* Create the checkmark/indicator (hidden when not checked) */

.checkmark:after {
  content: "";
  position: absolute;
  transform: rotate(0deg);
  border: 0.1em solid black;
  left: 0;
  top: 0;
  width: 1.05em;
  height: 1.05em;
  border-radius: 0.25em;
  transition: all 0.25s, border-width 0.1s;
}


/* Show the checkmark when checked */

.container input:checked~.checkmark:after {
  left: 0.45em;
  top: 0.25em;
  width: 0.25em;
  height: 0.5em;
  border-color: #fff0 white white #fff0;
  border-width: 0 0.15em 0.15em 0;
  border-radius: 0em;
  transform: rotate(45deg);
}
<input tyoe="text" id="inputbutton"></input>
<button class="addtask" onclick="task()" alt="Add task">
        <i>A</i>
        <i>d</i>
        <i>d</i>
        <i>&nbsp;</i>
        <i>t</i>
        <i>a</i>
        <i>s</i>
        <i>k</i>
      </button>
<label class="container">
        <input type="checkbox"/>
        <div class="checkmark"></div>
      </label>
<ul id="tasklist"></ul>

javascript html css button checkbox
1个回答
0
投票

待办事项列表是一个经典,也是复制模板的技术。只需复制 html 并设置目标的

innerHTML
即可。

let userinput = [];


function task() {
  const inputText = document.getElementById("inputbutton").value;

  // Check if inputText already exists in userinput array
  if (userinput.includes(inputText)) {
    alert("Already added");
    return; // Exit function early if inputText already exists
  }

  var html = document.querySelector("#myTaskTemplate").innerHTML

  userinput.push(inputText);

  let tasklist = document.getElementById("tasklist");
  let listItem = document.createElement("li");
  // listItem.textContent = inputText;
  listItem.innerHTML = html
  
  // after applying html, you can adjust elements or attach events
  listItem.querySelector(".checkmark").textContent = inputText

  tasklist.appendChild(listItem);

}

/*
I also tried this:
   let checkmarks = document.querySelectorAll('.container .checkmark');
    checkmarks.forEach(checkmark => {
        checkmark.style.display = 'block';
    });
*/
* {
  box-sizing: border-box;
  margin: 0;
}

#inputbutton {
  color: hsl(210deg 100% 44%);
  border: 2px solid hsl(210deg 100% 44%);
  border-radius: 10px;
  padding: 10px 25px;
  background: transparent;
  max-width: 190px;
  margin-top: 4em;
  margin-left: 42em;
  display: inline-flex;
  position: relative;
}

ul li {
  list-style: none;
  font-size: 17px;
  padding: 12px 8px 50px;
  user-select: none;
  cursor: pointer;
}

.input:active {
  box-shadow: 2px 2px 15px #8707ff inset;
}

button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  height: 40px;
  position: relative;
  padding: 0px 11px;
  font-size: 18px;
  text-transform: uppercase;
  border: 0;
  background-color: hsl(210deg 100% 44%);
  border-radius: 12px;
  overflow: hidden;
  transition: 31ms cubic-bezier(.5, .7, .4, 1);
}

button:before {
  content: attr(alt);
  display: flex;
  align-items: center;
  justify-content: center;
  position: absolute;
  inset: 0;
  font-size: 15px;
  font-weight: bold;
  color: white;
  letter-spacing: 4px;
  opacity: 1;
}

button:active {
  box-shadow: none;
  transform: translateY(7px);
  transition: 35ms cubic-bezier(.5, .7, .4, 1);
}

button:hover:before {
  transition: all .0s;
  transform: translateY(100%);
  opacity: 0;
}

button i {
  color: white;
  font-size: 15px;
  font-weight: bold;
  letter-spacing: 4px;
  font-style: normal;
  transition: all 2s ease;
  transform: translateY(-20px);
  opacity: 0;
}

button:hover i {
  transition: all .2s ease;
  transform: translateY(0px);
  opacity: 1;
}

button:hover i:nth-child(1) {
  transition-delay: 0.045s;
}

button:hover i:nth-child(2) {
  transition-delay: calc(0.045s * 3);
}

button:hover i:nth-child(3) {
  transition-delay: calc(0.045s * 4);
}

button:hover i:nth-child(4) {
  transition-delay: calc(0.045s * 5);
}

button:hover i:nth-child(6) {
  transition-delay: calc(0.045s * 6);
}

button:hover i:nth-child(7) {
  transition-delay: calc(0.045s * 7);
}

button:hover i:nth-child(8) {
  transition-delay: calc(0.045s * 8);
}

button:hover i:nth-child(9) {
  transition-delay: calc(0.045s * 9);
}

button:hover i:nth-child(10) {
  transition-delay: calc(0.045s * 10);
}


/* Hide the default checkbox */

.container input {
  display: none;
}

.container {
  display: block;
  position: relative;
  cursor: pointer;
  font-size: 20px;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}


/* Create a custom checkbox */

.checkmark {
  position: relative;
  top: 0;
  left: 0;
  height: 1.3em;
  width: 1.3em;
  background-color: #2196F300;
  border-radius: 0.25em;
  transition: all 0.25s;
}


/* When the checkbox is checked, add a blue background */

.container input:checked~.checkmark {
  background-color: #2196F3;
}


/* Create the checkmark/indicator (hidden when not checked) */

.checkmark:after {
  content: "";
  position: absolute;
  transform: rotate(0deg);
  border: 0.1em solid black;
  left: 0;
  top: 0;
  width: 1.05em;
  height: 1.05em;
  border-radius: 0.25em;
  transition: all 0.25s, border-width 0.1s;
}


/* Show the checkmark when checked */

.container input:checked~.checkmark:after {
  left: 0.45em;
  top: 0.25em;
  width: 0.25em;
  height: 0.5em;
  border-color: #fff0 white white #fff0;
  border-width: 0 0.15em 0.15em 0;
  border-radius: 0em;
  transform: rotate(45deg);
}
<input type="text" id="inputbutton">
<button class="addtask" onclick="task()" alt="Add task">
  <i>A</i>
  <i>d</i>
  <i>d</i>
  <i>&nbsp;</i>
  <i>t</i>
  <i>a</i>
  <i>s</i>
  <i>k</i>
</button>
<div id="myTaskTemplate" style="display:none">
  <label class="container">
    <input type="checkbox" />
    <div class="checkmark"></div>
  </label>
</div>
<ul id="tasklist"></ul>

© www.soinside.com 2019 - 2024. All rights reserved.