1 回答
TA贡献1860条经验 获得超9个赞
您可以添加一个onclick处理程序,然后单击按钮获取parentNode并使用remove将其删除
var button = document.querySelector("button");
var input = document.querySelector("input");
var ul = document.querySelector("ul");
button.addEventListener("click", function() {
if ((input.value.length) > 0 && (input.value.includes("by") === true)) {
var li = document.createElement("li");
li.appendChild(document.createTextNode(input.value + " "));
ul.appendChild(li);
input.value = "";
var nb = document.createElement("button");
//nb.classList.add('remove');
nb.onclick = removeThis; // added onclick handler
nb.appendChild(document.createTextNode("REMOVE"));
li.appendChild(nb);
} else if (input.value.includes("by") === false) {
alert("Make Sure You Adhere to the Defined Format");
}
});
function removeThis() {
this.parentNode.remove();
}
<ul></ul>
<input type='text'><button type='button'>Add</button>
添加回答
举报