简体   繁体   English

消除<p>点击元素</p>

[英]Remove <p> elements on click

I have this script:我有这个脚本:

var text = ["Ground Beef", "Spaghetti", "Tomatoes", "Onions", "Cheese"];
text.forEach(function(el) {
    var div = document.createElement("p");
    div.className = "list";
    div.innerHTML = el;
    document.body.appendChild(div);
});

Please let me know how to add on click function to delete each for this elements Ground Beef....请让我知道如何添加单击 function 以删除此元素的每个碎牛肉....

You could assign click event via onclick attribute.您可以通过onclick属性分配点击事件。 Then remove the element via e.target然后通过e.target移除元素

 var text = ["Ground Beef", "Spaghetti", "Tomatoes", "Onions", "Cheese"]; text.forEach(function(el) { var div = document.createElement("p"); div.className = "list"; div.innerHTML = el; div.onclick = remover document.body.appendChild(div); }); function remover(e){ e.target.remove() }

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

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