函数不会更新变量

时间:2019-10-14 16:56:11

标签: javascript html global-variables

变量status设置为“取消选中”,需要使用change()函数将其更新为“完成”。

最终需要执行的操作当按下ID为randomIdTwo的按钮时,会调用completeTodo()函数,该函数会导致将列表项从其当前的div“列表”中删除,并附加到div“ complete- list”,然后change()函数将“ status”变量的值从“ uncheck”更新为“ done”。

change()功能外,其他所有功能都可以使用。

document.getElementById('button').addEventListener('click', function addTodo() {
  var value = document.getElementById('input').value;
  var status = "uncheck";
  var randomId = Math.random();
  var randomIdTwo = Math.random();

  function change() {
    status = "done";
  };

  const item = `<li>
                    <div class="item">
                       <div class="complete">
                        <button id="` + randomIdTwo + `" class="${status}"></button>
                       </div>
    
                       <p class="text">${value}</p>
    
                        <div class="remove">
                           <button id="` + randomId + `" class="todo"></button>
                        </div>
                     </div>
                  </li>`;
  const position = "beforeend";

  list.insertAdjacentHTML(position, item);
  document.getElementById(randomId).addEventListener('click', function removeTodo() {
    var item = this.closest('li');
    item.remove();
  });
  document.getElementById(randomIdTwo).addEventListener('click', function completeTodo() {
    var item = this.closest('li');
    item.remove();
    document.getElementById("completelist").appendChild(item);
    change();
  });
});
<!DOCTYPE html>
<html lang="en" dir="ltr">

<head>
  <meta charset="utf-8">
  <title></title>
  <link rel="stylesheet" type="text/css" href="Resources/CSS/reset.min.css">
  <link rel="stylesheet" type="text/css" href="Resources/CSS/style.css">
</head>

<body>
  <div class="container">
    <header>
      <div id="datetime"></div>
      <div id="ampm"></div>
      <input type="text" id="input" placeholder="Add an item" />
      <button id="button" type="button"><img src="./img/additem4.png"></button>
    </header>
    <div id="list">
    </div>

    <div id="divline"></div>

    <div id="completelist">
    </div>
  </div>
  <script src="resources/JS/app.js"></script>
</body>

</html>

2 个答案:

答案 0 :(得分:1)

更改status变量不会更改元素的类。您需要更新按钮的classList

document.getElementById('button').addEventListener('click', function addTodo() {
  var value = document.getElementById('input').value;
  var status = "uncheck";
  var randomId = Math.random();
  var randomIdTwo = Math.random();

  function change(button) {
    button.classList.add("done");
    button.classList.remove("uncheck");
  };

  const item = `<li>
                    <div class="item">
                       <div class="complete">
                        <button id="` + randomIdTwo + `" class="${status}"></button>
                       </div>
    
                       <p class="text">${value}</p>
    
                        <div class="remove">
                           <button id="` + randomId + `" class="todo"></button>
                        </div>
                     </div>
                  </li>`;
  const position = "beforeend";

  list.insertAdjacentHTML(position, item);
  document.getElementById(randomId).addEventListener('click', function removeTodo() {
    var item = this.closest('li');
    item.remove();
  });
  document.getElementById(randomIdTwo).addEventListener('click', function completeTodo() {
    var item = this.closest('li');
    item.remove();
    document.getElementById("completelist").appendChild(item);
    change(this);
  });
});
<!DOCTYPE html>
<html lang="en" dir="ltr">

<head>
  <meta charset="utf-8">
  <title></title>
  <link rel="stylesheet" type="text/css" href="Resources/CSS/reset.min.css">
  <link rel="stylesheet" type="text/css" href="Resources/CSS/style.css">
</head>

<body>
  <div class="container">
    <header>
      <div id="datetime"></div>
      <div id="ampm"></div>
      <input type="text" id="input" placeholder="Add an item" />
      <button id="button" type="button"><img src="./img/additem4.png"></button>
    </header>
    <div id="list">
    </div>

    <div id="divline"></div>

    <div id="completelist">
    </div>
  </div>
  <script src="resources/JS/app.js"></script>
</body>

</html>

答案 1 :(得分:0)

您最初使用字符串插值设置类,但是字符串插值不会在结果元素和变量之间创建数据绑定。因此,将调用change()并更新状态,但是通过字符串创建的元素的值看不到该更改,因此未更新。

您需要访问DOM中的元素并手动更改其classList。