简体   繁体   English

如何 select 或给 class 名称以在 Li 元素中创建TextNode() tekst

[英]how to select or give a class name to createTextNode() tekst inside Li elements

I made a shopping list, now I want to strike through the items when I click on it.我做了一个购物清单,现在我想点击它时删除这些物品。 When I click on it now, the whole li item gets striked through(the marker and the deletebutton gets striked through now).当我现在单击它时,整个 li 项目被删除(标记和deletebutton按钮现在被删除)。 I want to give the tekst in the list items I created in this script a class, but I can't get it working.我想给我在这个脚本中创建的列表项中的 tekst 一个 class,但我无法让它工作。 this is the script where the list items is made:这是制作列表项的脚本:

    function createListElement() {
      var li = document.createElement("li");
      li.appendChild(document.createTextNode(input.value));
      li.classList.add("tekstje");
      ol.appendChild(li);
      input.value = "";
      
      var delBtn = document.createElement("BUTTON");  // Create a <button> element
      delBtn.classList.add("deletebtn");              // Give a class name
      delBtn.innerHTML = "X";                         // Insert text
      li.appendChild(delBtn);                         // Append <button> to <LI>
    }

I tried the classlist.add method and giving it a P class, but that doesn't seem to work.我尝试了classlist.add方法并给它一个 P class,但这似乎不起作用。 I also tried selecting it with:tekst我还尝试使用:tekst 选择它

Override the CSS on the inner button.覆盖内部按钮上的 CSS。 Or, just wrap the text within the li with a span and only add the strike through on the span .或者,只需用 span 将文本包装在li中,然后只在span上添加删除线。

 var ol = document.getElementById("theOl"); var input = document.getElementById("theInput"); function createListElement() { var li = document.createElement("li"); li.appendChild(document.createTextNode(input.value)); li.classList.add("tekstje"); ol.appendChild(li); input.value = ""; var delBtn = document.createElement("BUTTON"); // Create a <button> element delBtn.classList.add("deletebtn"); // Give a class name delBtn.innerHTML = "X"; // Insert text delBtn.onclick = function () { delBtn.parentNode.classList.add("all-done"); } li.appendChild(delBtn); // Append <button> to <LI> }
 .all-done { text-decoration: line-through; }.all-done button { text-decoration: none; }
 <ol id="theOl"></ol> <input id="theInput" value="test"> <button onclick="createListElement()">Add</button>

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

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