我如何 space 退出复选框、列表和删除按钮

How do I space out the checkbox, list and delete button

我制作了一个待办事项列表,所有功能都可用,但我不知道如何在输入和删除按钮之间放置空格,然后像复选框一样对齐删除按钮。输入许多输入后,整个列表看起来很混乱。

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

// adds input Item to list
function addItem(list, input) {
  var inputItem = this.inputItem;
  var list = document.getElementById(list);
  var listItem = document.createElement("li");

  // Configure the delete button
  var deleteButton = document.createElement("button");
  deleteButton.innerText = "X";
  deleteButton.addEventListener("click", function() {
    this.parentElement.style.display = 'none';
  });

  // Configure the check box
  var checkBox = document.createElement("input");
  checkBox.id = "check";
  checkBox.type = 'checkbox';
  checkBox.addEventListener('change', function() {
    labelText.style.textDecoration = checkBox.checked ? 'line-through' : 'none';
  });

  // Configure the label
  var label = document.createElement("label");
  var labelText = document.createElement("span");
  labelText.innerText = input.value;

  // Put the checkbox and label text in to the label element
  label.appendChild(checkBox);
  label.appendChild(labelText);

  // Put the label (with the checkbox inside) and the delete
  // button into the list item.
  listItem.appendChild(label);
  listItem.appendChild(deleteButton);

  list.appendChild(listItem);
  inputItem.focus();
  inputItem.select();
  return false;

}
localStorage.setItem("list", list);

localStorage.getItem("list").forEach(function(list) {
  elem.textContent = list;
});
body {
  text-align: center;
}

form {
  display: inline-block;
}

#outerDiv {
  padding: 30px;
  text-align: center;
}

#innerDiv {
  margin: auto;
  width: 200px;
  height: 100px;
}

ul {
  padding: 0;
  margin: 0;
}

ul li {
  position: relative;
  padding: 12px 8px 12px 40px;
  background: rgb(148, 160, 181);
  list-style-type: none;
  font-size: 18px;
}

#submit {
  position: absolute;
  padding: 10px 16px;
}


/* Style the input */

input {
  margin: 0;
  border: none;
  border-radius: 0;
  padding: 10px;
  float: left;
  font-size: 16px;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
</head>

<h1 align="center"> To-Do List </h1>

<body>
  <div id="outerDiv">
    <form onsubmit="return addItem('list', this.inputItem)">
      <input type="text" id="inputItem" onfocus="this.value=''" onselect="this.value=''" placeholder="Enter a Task">
      <input id="submit" type="submit">
    </form>
  </div>

  <div id="innerDiv">
    <ul id="list"></ul>
  </div>

  <script>
    var inputItem = document.getElementById("inputItem");
    inputItem.focus();

    // adds input Item to list
    function addItem(list, input) {
      var inputItem = this.inputItem;
      var list = document.getElementById(list);
      var listItem = document.createElement("li");

      // Configure the delete button
      var deleteButton = document.createElement("button");
      deleteButton.innerText = "X";
      deleteButton.addEventListener("click", function() {
        this.parentElement.style.display = 'none';
      });

      // Configure the check box
      var checkBox = document.createElement("input");
      checkBox.id = "check";
      checkBox.type = 'checkbox';
      checkBox.addEventListener('change', function() {
        labelText.style.textDecoration = checkBox.checked ? 'line-through' : 'none';
      });

      // Configure the label
      var label = document.createElement("label");
      var labelText = document.createElement("span");
      labelText.innerText = input.value;

      // Put the checkbox and label text in to the label element
      label.appendChild(checkBox);
      label.appendChild(labelText);

      // Put the label (with the checkbox inside) and the delete
      // button into the list item.
      listItem.appendChild(label);
      listItem.appendChild(deleteButton);

      list.appendChild(listItem);
      inputItem.focus();
      inputItem.select();
      return false;

    }
    localStorage.setItem("list", list);

    localStorage.getItem("list").forEach(function(list) {
      elem.textContent = list;
    });
  </script>

</body>

</html>

我们又见面了。部分问题在于你将所有偏离你风格的东西置于中心位置。除了你的#outerDiv,我删除了居中。然后,我在复选框的右边留了一点边距,这样它们就不会靠得太近。最后,我将按钮向右浮动,以便它们始终右对齐。

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

// adds input Item to list
function addItem(list, input) {
  var inputItem = this.inputItem;
  var list = document.getElementById(list);
  var listItem = document.createElement("li");

  // Configure the delete button
  var deleteButton = document.createElement("button");
  deleteButton.innerText = "X";
  deleteButton.addEventListener("click", function() {
    this.parentElement.style.display = 'none';
  });

  // Configure the check box
  var checkBox = document.createElement("input");
  checkBox.id = "check";
  checkBox.type = 'checkbox';
  checkBox.addEventListener('change', function() {
    labelText.style.textDecoration = checkBox.checked ? 'line-through' : 'none';
  });

  // Configure the label
  var label = document.createElement("label");
  var labelText = document.createElement("span");
  labelText.innerText = input.value;

  // Put the checkbox and label text in to the label element
  label.appendChild(checkBox);
  label.appendChild(labelText);

  // Put the label (with the checkbox inside) and the delete
  // button into the list item.
  listItem.appendChild(label);
  listItem.appendChild(deleteButton);

  list.appendChild(listItem);
  inputItem.focus();
  inputItem.select();
  return false;

}
//localStorage.setItem("list", list);

//localStorage.getItem("list").forEach(function(list) {
//  elem.textContent = list;
//});
body {}

form {
  display: inline-block;
}

#outerDiv {
  padding: 30px;
  text-align: center;
}

#innerDiv {
  margin: auto;
  width: 200px;
  height: 100px;
}

ul {
  padding: 0;
  margin: 0;
}

ul li {
  position: relative;
  padding: 12px 8px 12px 20px;
  background: rgb(148, 160, 181);
  list-style-type: none;
  font-size: 18px;
}

#submit {
  position: absolute;
  padding: 10px 16px;
}


/* Style the input */

button {
  float: right;
}

input {
  margin: 0;
  border: none;
  border-radius: 0;
  padding: 10px;
  float: left;
  font-size: 16px;
  margin-right: 8px;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
</head>

<h1 align="center"> To-Do List </h1>

<body>
  <div id="outerDiv">
    <form onsubmit="return addItem('list', this.inputItem)">
      <input type="text" id="inputItem" onfocus="this.value=''" onselect="this.value=''" placeholder="Enter a Task">
      <input id="submit" type="submit">
    </form>
  </div>

  <div id="innerDiv">
    <ul id="list"></ul>
  </div>
</body>

</html>

我在 css 中为您创建了一些新的 类,它们位于 #innerDiv 下的工作片段中。 (还有一个提示,您不需要将 css 和脚本添加到代码段中的 html 文件中)。希望对您有所帮助!

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

// adds input Item to list
function addItem(list, input) {
  var inputItem = this.inputItem;
  var list = document.getElementById(list);
  var listItem = document.createElement("li");

  // Configure the delete button
  var deleteButton = document.createElement("button");
  deleteButton.innerText = "X";
  deleteButton.addEventListener("click", function() {
    this.parentElement.style.display = 'none';
  });

  // Configure the check box
  var checkBox = document.createElement("input");
  checkBox.id = "check";
  checkBox.type = 'checkbox';
  checkBox.addEventListener('change', function() {
    labelText.style.textDecoration = checkBox.checked ? 'line-through' : 'none';
  });

  // Configure the label
  var label = document.createElement("label");
  var labelText = document.createElement("span");
  labelText.innerText = input.value;

  // Put the checkbox and label text in to the label element
  label.appendChild(checkBox);
  label.appendChild(labelText);

  // Put the label (with the checkbox inside) and the delete
  // button into the list item.
  listItem.appendChild(label);
  listItem.appendChild(deleteButton);

  list.appendChild(listItem);
  inputItem.focus();
  inputItem.select();
  return false;

}
//localStorage.setItem("list", list);

//localStorage.getItem("list").forEach(function(list) {
//  elem.textContent = list;
//});
body {
  text-align: center;
}

form {
  display: inline-block;
}

#outerDiv {
  padding: 30px;
  text-align: center;
}

#innerDiv {
  margin: auto;
  width: 200px;
  height: 100px;
}

#innerDiv button {
    margin: 0 5px;
    padding: 2px;
    position: absolute;
    right: 0;
}

#innerDiv li {
    margin: 0;
    padding: 4px 0px 0px 4px;
    text-align: left;
}

#innerDiv label {
  padding-left: 4px;
}

ul {
  padding: 0;
  margin: 0;
}

ul li {
  position: relative;
  padding: 12px 8px 12px 40px;
  background: rgb(148, 160, 181);
  list-style-type: none;
  font-size: 18px;
}

#submit {
  position: absolute;
  padding: 10px 16px;
}


/* Style the input */

input {
  margin: 0;
  border: none;
  border-radius: 0;
  padding: 10px;
  float: left;
  font-size: 16px;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
</head>

<h1 align="center"> To-Do List </h1>

<body>
  <div id="outerDiv">
    <form onsubmit="return addItem('list', this.inputItem)">
      <input type="text" id="inputItem" onfocus="this.value=''" onselect="this.value=''" placeholder="Enter a Task">
      <input id="submit" type="submit">
    </form>
  </div>

  <div id="innerDiv">
    <ul id="list"></ul>
  </div>
</body>

</html>

  1. 先在删除按钮上加一个class

    deleteButton.classList.add("delete-button");

  2. 比在 CSS 中尽可能多地使用 class 设置按钮样式。

** 您还必须处理您的待办事项文本。如果文本较长,样式就会中断。您可以在此处应用相同的技术 - 使用 JS 添加 class,在 CSS.

中设置 class 的样式

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

// adds input Item to list
function addItem(list, input) {
  var inputItem = this.inputItem;
  var list = document.getElementById(list);
  var listItem = document.createElement("li");

  // Configure the delete button
  var deleteButton = document.createElement("button");
  deleteButton.classList.add("delete-button");
  deleteButton.innerText = "X";
  deleteButton.addEventListener("click", function() {
    this.parentElement.style.display = 'none';
  });

  // Configure the check box
  var checkBox = document.createElement("input");
  checkBox.id = "check";
  checkBox.type = 'checkbox';
  checkBox.addEventListener('change', function() {
    labelText.style.textDecoration = checkBox.checked ? 'line-through' : 'none';
  });

  // Configure the label
  var label = document.createElement("label");
  var labelText = document.createElement("span");
  labelText.innerText = input.value;

  // Put the checkbox and label text in to the label element
  label.appendChild(checkBox);
  label.appendChild(labelText);

  // Put the label (with the checkbox inside) and the delete
  // button into the list item.
  listItem.appendChild(label);
  listItem.appendChild(deleteButton);

  list.appendChild(listItem);
  inputItem.focus();
  inputItem.select();
  return false;

}
localStorage.setItem("list", list);

localStorage.getItem("list").forEach(function(list) {
  elem.textContent = list;
});
body {
  text-align: center;
}

form {
  display: inline-block;
}

#outerDiv {
  padding: 30px;
  text-align: center;
}

#innerDiv {
  margin: auto;
  width: 200px;
  height: 100px;
}

ul {
  padding: 0;
  margin: 0;
}

ul li {
  position: relative;
  padding: 12px 8px 12px 40px;
  background: rgb(148, 160, 181);
  list-style-type: none;
  font-size: 18px;
}

#submit {
  position: absolute;
  padding: 10px 16px;
}


/* Style the input */

input {
  margin: 0;
  border: none;
  border-radius: 0;
  padding: 10px;
  float: left;
  font-size: 16px;
}
.delete-button {
  float: right;
  border: 0;
  padding: 1px 3px 0;
  font-size: 9px;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
</head>

<h1 align="center"> To-Do List </h1>

<body>
  <div id="outerDiv">
    <form onsubmit="return addItem('list', this.inputItem)">
      <input type="text" id="inputItem" onfocus="this.value=''" onselect="this.value=''" placeholder="Enter a Task">
      <input id="submit" type="submit">
    </form>
  </div>

  <div id="innerDiv">
    <ul id="list"></ul>
  </div>

  <script>
    var inputItem = document.getElementById("inputItem");
    inputItem.focus();

    // adds input Item to list
    function addItem(list, input) {
      var inputItem = this.inputItem;
      var list = document.getElementById(list);
      var listItem = document.createElement("li");

      // Configure the delete button
      var deleteButton = document.createElement("button");
      deleteButton.innerText = "X";
      deleteButton.addEventListener("click", function() {
        this.parentElement.style.display = 'none';
      });

      // Configure the check box
      var checkBox = document.createElement("input");
      checkBox.id = "check";
      checkBox.type = 'checkbox';
      checkBox.addEventListener('change', function() {
        labelText.style.textDecoration = checkBox.checked ? 'line-through' : 'none';
      });

      // Configure the label
      var label = document.createElement("label");
      var labelText = document.createElement("span");
      labelText.innerText = input.value;

      // Put the checkbox and label text in to the label element
      label.appendChild(checkBox);
      label.appendChild(labelText);

      // Put the label (with the checkbox inside) and the delete
      // button into the list item.
      listItem.appendChild(label);
      listItem.appendChild(deleteButton);

      list.appendChild(listItem);
      inputItem.focus();
      inputItem.select();
      return false;

    }
    localStorage.setItem("list", list);

    localStorage.getItem("list").forEach(function(list) {
      elem.textContent = list;
    });
  </script>

</body>

</html>