移除点击的 <li>点击

Remove clicked lt;ligt; onclick(移除点击的 lt;ligt;点击)

本文介绍了移除点击的 <li>点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个 JavaScript 代码:

I have this JavaScript code:

window.onload = init;

function init () {
    var button = document.getElementById("submitButton");
    button.onclick = addItem;
    var listItems = document.querySelectorAll("li");  //assigning the remove click event to all list items
    for (var i = 0; i < listItems.length; i++) {
        listItems[i].onclick = li.parentNode.removeChild(li);
    }
}

function addItem() {
    var textInput = document.getElementById("item");  //getting text input
    var text = textInput.value;   //getting value of text input element
    var ul = document.getElementById("ul");  //getting element <ul> to add element to
    var li = document.createElement("li");  //creating li element to add
    li.innerHTML = text;    //inserting text into newly created <li> element

    if (ul.childElementCount == 0) {  //using if/else statement to add items to top of list
        ul.appendChild(li);       // will add if count of ul children is 0 otherwise add before first item
    }
    else {
        ul.insertBefore(li, ul.firstChild);
    }
}

function remove(e) {
    var li = e.target;
    var listItems = document.querySelectorAll("li"); 
    var ul = document.getElementById("ul");
    li.parentNode.removeChild(li);        
}

还有这个 HTML:

<body>
     <form>
         <label for="item">Add an item: </label>
          <input id="item" type="text" size="20"><br>
         <input id="submitButton" type="button" value="Add!">
     </form>
     <ul id="ul">
     </ul>
     <p>
         Click an item to remove it from the list.
    </p>  
</body>

我想要做的是删除用户单击的任何 <li> 元素,但这似乎不起作用,我无法在网上其他任何地方找到答案具体场景.希望有人可以在这里帮助我并告诉我我缺少什么.

What I want to do is remove the whichever <li> element the user clicks, but this doesn't seem to be working and I am unable to find an answer anywhere else online for this specific scenario. Hoping someone can help me out here and show me what i am missing.

推荐答案

更新

纯 JS 委托

将 eventListener 添加到 UL 以在动态插入的 LI 上委托点击:

Add the eventListener to the UL to delegate the click even on dynamically inserted LIs:

document.getElementById("ul").addEventListener("click",function(e) {
  var tgt = e.target;
  if (tgt.tagName.toUpperCase() == "LI") {
    tgt.parentNode.removeChild(tgt); // or tgt.remove();
  }
});

jQuery 委托

$(function() {
  $("#submitButton").on("click",function() {
    var text = $("#item").val();   //getting value of text input element
    var li = $('<li/>').text(text)
    $("#ul").prepend(li); 
  });
  $("#ul").on("click","li",function() {
    $(this).remove();
  });
});   

原答案

因为你没有提到 jQuery

Since you did not mention jQuery

var listItems = document.getElementsByTagName("li"); // or document.querySelectorAll("li"); 
for (var i = 0; i < listItems.length; i++) {
  listItems[i].onclick = function() {this.parentNode.removeChild(this);}
}

你可能想把它包起来

window.onload=function() { // or addEventListener
  // do stuff to the DOM here
}

重新阅读我认为您还想将其添加到动态 LI 的问题

Re-reading the question I think you also want to add that to the dynamic LIs

li.innerHTML = text;    //inserting text into newly created <li> element
li.onclick = function() {
  this.parentNode.removeChild(this);
   // or this.remove(); if supported
}

这是我希望您编写的完整代码

Here is the complete code as I expect you meant to code it

现场演示

window.onload=function() {
  var button = document.getElementById("submitButton");
  button.onclick = addItem;
}   

function addItem() {
  var textInput = document.getElementById("item");  //getting text input
  var text = textInput.value;   //getting value of text input element
  var ul = document.getElementById("ul");  //getting element <ul> to add element to
  var li = document.createElement("li");  //creating li element to add
  li.innerHTML = text;    //inserting text into newly created <li> element
  li.onclick = function() {
    this.parentNode.removeChild(this);
    // or this.remove(); if supported
  }
  if (ul.childElementCount == 0) {  //using if/else statement to add items to top of list
    ul.appendChild(li); // will add if count of ul children is 0 otherwise add before first item
  }
  else {
    ul.insertBefore(li, ul.firstChild);
  }
}

如果你想使用 jQuery,整个事情会变得更简单

In case you want to use jQuery, the whole thing gets somewhat simpler

现场演示

$(function() {
    $("#submitButton").on("click",function() {
        var text = $("#item").val();   //getting value of text input element
        var li = $('<li/>')
          .text(text)
          .on("click",function() { $(this).remove()});
        $("#ul").prepend(li); 
    });
});   

这篇关于移除点击的 &lt;li&gt;点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:移除点击的 &lt;li&gt;点击

基础教程推荐