猿问

在我现有的 html 列表中使用 javascript 添加删除按钮

我想在 javascript 中添加一个函数,该函数在添加到我的列表中的新项目旁边添加一个删除按钮,我还希望删除按钮出现在我在 html 中创建的现有列表项旁边,当我按下删除时按钮它会删除列表中的该项目。


HTML


<!DOCTYPE html>

<html>

<head>

    <title>Javascript + DOM</title>

    <link rel="stylesheet" type="text/css" href="style.css">

</head>

<body>

    <h1>Shopping List</h1>

    <p id="first">Get it done today</p>

    <input id="userinput" type="text" placeholder="enter items">

    <button id="enter">Enter</button>

    <ul>

        <li class="bold red" random="23">Notebook</li>

        <li>Jello</li>

        <li>Spinach</li>

        <li>Rice</li>

        <li>Birthday Cake</li>

        <li>Candles</li>

    </ul>

    <script type="text/javascript" src="script.js"></script>

</body>

</html>

JS


var button = document.getElementById("enter");

var input = document.getElementById("userinput");

var ul = document.querySelector("ul");


function inputLength() {

    return input.value.length;

}


function createListElement() {

    var li = document.createElement("li");

    li.appendChild(document.createTextNode(input.value));

    ul.appendChild(li);

    input.value = "";

}


function addListAfterClick() {

    if (inputLength() > 0) {

        createListElement();

    }

}


function addListAfterKeypress(event) {

    if (inputLength() > 0 && event.keyCode === 13) {

        createListElement();

    }

}


button.addEventListener("click", addListAfterClick);


input.addEventListener("keypress", addListAfterKeypress);


慕沐林林
浏览 210回答 2
2回答

侃侃无极

var button = document.getElementById("enter");var input = document.getElementById("userinput");var ul = document.querySelector("ul");var shoppingListItems = ["Notebook", "Jello", "Spinach", "Rice", "Birthday Cake", "Candles"]function inputLength() {&nbsp; return input.value.length;}function createListElement(text, index) {&nbsp; var li = document.createElement("li");&nbsp; var deleteBtn = document.createElement("button");&nbsp; deleteBtn.itemId = index&nbsp; deleteBtn.onclick = deleteItem.bind(null, index)&nbsp; deleteBtn.appendChild(document.createTextNode("Delete"))&nbsp; li.appendChild(document.createTextNode(text));&nbsp; li.appendChild(deleteBtn);&nbsp; ul.appendChild(li);}function addListAfterClick() {&nbsp; &nbsp; if (inputLength() > 0) {&nbsp; &nbsp; &nbsp; &nbsp; shoppingListItems.push(input.value)&nbsp; &nbsp; &nbsp; createListElement(input.value, shoppingListItems.length - 1);&nbsp; &nbsp; }}function addListAfterKeypress(event) {&nbsp; &nbsp; if (inputLength() > 0 && event.keyCode === 13) {&nbsp; &nbsp; &nbsp; shoppingListItems.push(input.value)&nbsp; &nbsp; &nbsp; createListElement(input.value, shoppingListItems.length - 1);&nbsp; &nbsp; }}function deleteItem(itemId) {&nbsp; var itemXpath = "//li[text()='"+shoppingListItems[itemId]+ "']";&nbsp; &nbsp; var item = document.evaluate(itemXpath, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;&nbsp; item.remove();}function initPage() {&nbsp; shoppingListItems.forEach(function(element, index) {&nbsp; &nbsp; createListElement(element, index)&nbsp; })}button.addEventListener("click", addListAfterClick);input.addEventListener("keypress", addListAfterKeypress);window.onload = initPage;<!DOCTYPE html><html><head>&nbsp; &nbsp; <title>Javascript + DOM</title>&nbsp; &nbsp; <link rel="stylesheet" type="text/css" href="style.css"></head><body>&nbsp; &nbsp; <h1>Shopping List</h1>&nbsp; &nbsp; <p id="first">Get it done today</p>&nbsp; &nbsp; <input id="userinput" type="text" placeholder="enter items">&nbsp; &nbsp; <button id="enter">Enter</button>&nbsp; &nbsp; <ul></ul>&nbsp; &nbsp; <script type="text/javascript" src="script.js"></script></body></html>

繁星点点滴滴

你可以这样做:HTML:<ul>&nbsp; &nbsp; &nbsp; &nbsp; <li class="bold red" random="23">Notebook</li>&nbsp; &nbsp; &nbsp; &nbsp; <li id=li1>Jello <input id='btn1' type="submit" value='-'></input></li>&nbsp; &nbsp; &nbsp; &nbsp; <li>Spinach</li>&nbsp; &nbsp; &nbsp; &nbsp; <li>Rice</li>&nbsp; &nbsp; &nbsp; &nbsp; <li>Birthday Cake</li>&nbsp; &nbsp; &nbsp; &nbsp; <li>Candles</li></ul>Javascript:var btn = document.getElementById('btn1');btn.onclick = function () {&nbsp; &nbsp; document.getElementById('li1').remove();&nbsp; &nbsp; this.remove();};你可以玩这个例子:https://jsfiddle.net/be4psyrL/
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答