繁体   English   中英

如果选中复选框,如何对文本进行换行

[英]how to linethrough a text if checkbox is checked

我正在尝试为每个元素制作一个带有复选框的待办事项列表。 当复选框被选中时,我希望我的元素被设置为直通样式。

这是我的代码

我有这个 HTML

<h1>My List</h1>
<input id="item" type="text" />
<button id="add">Add</button>
<ul id="toDo"></ul>

和js

document.addEventListener('DOMContentLoaded', function () {
    const addButton = document.querySelector('#addButton');

    addButton.addEventListener('click', function () {
        const textInput = document.querySelector('#itemText');
        const itemText  = textInput.value;

        if (itemText) {
            const ul = document.querySelector('ul');
            const li = document.createElement('li');

            li.addEventListener('click', function (e) {
              if (e.target!=checkbox){
                e.target.parentNode.removeChild(e.target);
              }

              //i try do use this
              if (e.target==checkbox){
              if (checkbox.checked == true){
               e.target.parentNode.textContent.style.textDecoration = "linethrough";

               } 
              }


            });


          var checkbox = document.createElement('input');
          checkbox.type = "checkbox";
          checkbox.value = 0;

          li.appendChild(checkbox);
          li.appendChild(document.createTextNode(itemText));

           ul.appendChild(li);
           textInput.value = '';
        }
    });
});

我得到 Uncaught TypeError: Cannot set property 'textDecoration' of undefined。 如何设置我单击的复选框旁边的文本样式?

e.target.parentNode.textContent 是纯字符串,因此在这种情况下您不能使用 style 和 textDecoration。

也使用“line-through”而不是“linethrough”

所以使用下面的就可以了!

e.target.parentNode.style.textDecoration = "line-through";

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM