英文:
How To Only Address One Element In A Parent Div
问题
我正在处理一个简单的待办事项列表,当我点击新项目旁边的复选框时,文本和删除按钮都会带有删除线属性。有没有办法让只有文本带有删除线,而不是删除按钮?
我已经尝试过针对特定子元素进行定位,但我仍在学习,可能做错了。我无法弄清如何只针对文本项目,而不是删除按钮。
代码片段
let taskList = document.getElementById('taskList');
let taskBox = document.getElementById('taskBox');
let taskCount = 0;
document.addEventListener('keypress', function(e) {
if (e.key === 'Enter') {
addTask();
}
});
function addTask() {
let taskVal = taskBox.value;
if (taskVal.length < 1) {
window.alert('Task Box Empty');
} else {
let itemDiv = document.createElement('li');
let itemCheck = document.createElement('input');
let textItem = document.createTextNode(taskVal);
let itemDelete = document.createElement('button');
itemDiv.classList.add('itemDiv');
itemCheck.style.height = "50px";
itemCheck.type = "checkbox";
itemCheck.name = "finished";
itemCheck.id = "checkbox";
textItem.innerText = taskVal;
itemDelete.innerText = "x";
taskList.append(itemDiv);
itemDiv.appendChild(itemCheck);
itemDiv.appendChild(textItem);
itemDiv.appendChild(itemDelete);
taskCount++;
itemCheck.onclick = finishTask.bind(itemCheck);
itemDelete.onclick = deleteItem.bind(itemDiv);
document.getElementById('taskBox').innerHTML = "";
taskBox.value = "";
}
}
function finishTask() {
let children = this.parentNode.children;
if (this.checked) {
this.parentNode.style.textDecoration = "line-through";
} else {
this.parentNode.style.textDecoration = "none";
}
}
<h1>我的待办事项列表</h1>
<hr>
<div class="addTaskContainer">
<input id="taskBox" type="textarea" placeholder="添加任务" autocomplete="off">
<button class="add" onClick="addTask()">添加任务</button>
</div>
<ul class="taskList" id="taskList"></ul>
<button class="bottomBtn" onClick="clearAll()">清除所有</button>
<button class="bottomBtn" onClick="clearFinished(itemCheck)">清除已完成</button>
请注意,这只是代码片段的一部分,我已为您翻译出相关代码的部分。如果您需要更多帮助,请随时提出问题。
英文:
I am working on a simple to-do list and when I click the check box next to the new item, both the text and the delete button gain the strikethrough property. Is there a way for me to make it so that only the text gets the strikethrough, and not the delete button?
I have tried targeting specific children but I am still learning and might have done this wrong. I can't figure out how to only target the text item and not the delete button.
Snippet
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
let taskList = document.getElementById('taskList');
let taskBox = document.getElementById('taskBox');
let taskCount = 0;
document.addEventListener('keypress', function(e) {
if (e.key === 'Enter') {
addTask();
}
});
function addTask() {
let taskVal = taskBox.value;
if (taskVal.length < 1) {
window.alert('Task Box Empty');
} else {
let itemDiv = document.createElement('li');
let itemCheck = document.createElement('input');
let textItem = document.createTextNode(taskVal);
let itemDelete = document.createElement('button');
itemDiv.classList.add('itemDiv');
itemCheck.style.height = "50px";
itemCheck.type = "checkbox";
itemCheck.name = "finished";
itemCheck.id = "checkbox";
textItem.innerText = taskVal;
itemDelete.innerText = "x"
taskList.append(itemDiv);
itemDiv.appendChild(itemCheck);
itemDiv.appendChild(textItem);
itemDiv.appendChild(itemDelete);
taskCount++;
itemCheck.onclick = finishTask.bind(itemCheck);
itemDelete.onclick = deleteItem.bind(itemDiv);
document.getElementById('taskBox').innerHTML = "";
taskBox.value = "";
}
}
function finishTask() {
let children = this.parentNode.children;
if (this.checked) {
this.parentNode.style.textDecoration = "line-through";
} else {
this.parentNode.style.textDecoration = "none";
}
}
<!-- language: lang-html -->
<h1>My To-Do List</h1>
<hr>
<div class="addTaskContainer">
<input id="taskBox" type="textarea" placeholder="Add Task" autocomplete="off">
<button class="add" onClick="addTask()">Add Task</button>
</div>
<ul class="taskList" id="taskList"></ul>
<button class="bottomBtn" onClick="clearAll()">Clear All</button>
<button class="bottomBtn" onClick="clearFinished(itemCheck)">Clear Finished</button>
<!-- end snippet -->
答案1
得分: 1
最简单的方法是将您的文本包装在一个元素中。
首先,删除这一行:
let textItem = document.createTextNode(taskVal);
并用元素替换它:
let textItem = document.createElement('span');
textItem.innerText = taskVal;
然后在父节点中选择您的元素:
function finishTask() {
let textNode = this.parentNode.querySelector('span');
if (this.checked) {
textNode.style.textDecoration = 'line-through';
} else {
textNode.style.textDecoration = 'none';
}
// 或者简单地使用以下方式:
// let textNode = this.parentNode.querySelector('span');
// textNode.style.textDecoration = this.checked ? 'line-through' : 'none';
}
英文:
The easiest way would be to wrap your text in a span.
First, remove this line
let textItem = document.createTextNode(taskVal);
and replace it with a span
let textItem = document.createElement('span');
textItem.innerText = taskVal;
Then target span your span in parent node.
function finishTask() {
let textNode = this.parentNode.querySelector('span')
if (this.checked) {
textNode.style.textDecoration = 'line-through';
} else {
textNode.style.textDecoration = 'none';
}
// OR simply
// let textNode = this.parentNode.querySelector('span')
// textNode.style.textDecoration = this.checked ? 'line-through' : 'none';
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论