只处理父 div 中的一个元素。

huangapple go评论113阅读模式
英文:

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(&#39;taskList&#39;);
let taskBox = document.getElementById(&#39;taskBox&#39;);
let taskCount = 0;
document.addEventListener(&#39;keypress&#39;, function(e) {
if (e.key === &#39;Enter&#39;) {
addTask();
}
});
function addTask() {
let taskVal = taskBox.value;
if (taskVal.length &lt; 1) {
window.alert(&#39;Task Box Empty&#39;);
} else {
let itemDiv = document.createElement(&#39;li&#39;);
let itemCheck = document.createElement(&#39;input&#39;);
let textItem = document.createTextNode(taskVal);
let itemDelete = document.createElement(&#39;button&#39;);
itemDiv.classList.add(&#39;itemDiv&#39;);
itemCheck.style.height = &quot;50px&quot;;
itemCheck.type = &quot;checkbox&quot;;
itemCheck.name = &quot;finished&quot;;
itemCheck.id = &quot;checkbox&quot;;
textItem.innerText = taskVal;
itemDelete.innerText = &quot;x&quot;
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(&#39;taskBox&#39;).innerHTML = &quot;&quot;;
taskBox.value = &quot;&quot;;
}
}
function finishTask() {
let children = this.parentNode.children;
if (this.checked) {
this.parentNode.style.textDecoration = &quot;line-through&quot;;
} else {
this.parentNode.style.textDecoration = &quot;none&quot;;
}
}

<!-- language: lang-html -->

&lt;h1&gt;My To-Do List&lt;/h1&gt;
&lt;hr&gt;
&lt;div class=&quot;addTaskContainer&quot;&gt;
&lt;input id=&quot;taskBox&quot; type=&quot;textarea&quot; placeholder=&quot;Add Task&quot; autocomplete=&quot;off&quot;&gt;
&lt;button class=&quot;add&quot; onClick=&quot;addTask()&quot;&gt;Add Task&lt;/button&gt;
&lt;/div&gt;
&lt;ul class=&quot;taskList&quot; id=&quot;taskList&quot;&gt;&lt;/ul&gt;
&lt;button class=&quot;bottomBtn&quot; onClick=&quot;clearAll()&quot;&gt;Clear All&lt;/button&gt;
&lt;button class=&quot;bottomBtn&quot; onClick=&quot;clearFinished(itemCheck)&quot;&gt;Clear Finished&lt;/button&gt;

<!-- 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(&#39;span&#39;);
textItem.innerText = taskVal;

Then target span your span in parent node.

function finishTask() {
let textNode = this.parentNode.querySelector(&#39;span&#39;)
if (this.checked) {
textNode.style.textDecoration = &#39;line-through&#39;;
} else {
textNode.style.textDecoration = &#39;none&#39;;
}
// OR simply
//   let textNode = this.parentNode.querySelector(&#39;span&#39;)
//   textNode.style.textDecoration = this.checked ? &#39;line-through&#39; : &#39;none&#39;;
}

huangapple
  • 本文由 发表于 2023年3月3日 23:21:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/75628904.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定