如何间隔复选框,列表和删除按钮



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

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 下的工作片段中。(也是一个提示,您不需要在代码片段中的 html 文件中添加 css 和脚本(。希望对您有所帮助!

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. 首先将类添加到删除按钮

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

  2. 而不是在 CSS 中尽可能多地使用该类设置按钮样式。

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

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>

相关内容

  • 没有找到相关文章