gpt4 book ai didi

javascript - 移除点击的
  • onclick
  • 转载 作者:可可西里 更新时间:2023-11-01 01:27:20 25 4
    gpt4 key购买 nike

    我有这段 JavaScript 代码:

    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> 中的那个用户点击的元素,但这似乎不起作用,我无法在其他任何地方在线找到针对此特定场景的答案。希望有人可以帮助我,告诉我我缺少什么。

    最佳答案

    更新

    纯 JS 委托(delegate)

    将 eventListener 添加到 UL 以委托(delegate)对动态插入的 LI 的点击:

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

    jQuery 委托(delegate)

    $(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

    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 中

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

    这是完整的代码,我希望你能编写它

    Live Demo

    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,整个事情会变得更简单

    Live Demo

    $(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);
    });
    });

    关于javascript - 移除点击的 <li> onclick,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17012157/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com