简体   繁体   English

DOM:在onclick事件中删除新创建的“article”元素并使用新创建的删除按钮?

[英]DOM: Delete newly created 'article' element with newly created delete button within onclick event?

I have one section element that contains one article element. 我有一个包含一个文章元素的section元素。 Also, I have one input button with 'onclick' event. 另外,我有一个带有'onclick'事件的输入按钮。 Whenever this event fired, a new article element appended to the section element with unique id . 每当此事件触发时,都会在具有唯一ID的section元素后附加一个新的article元素。 The newArticle element contains a label, text box and a delete button. newArticle元素包含标签,文本框和删除按钮。 All these three elements get created within the on-click event. 所有这三个元素都在点击事件中创建。

document.getElementById("addRow").onclick = function () {

    var newCustomerlbl = document.createElement("label");
    newCustomerlbl.innerHTML = "Cutomer Name: ";
    var newCustomertxt = document.createElement("input");
    newCustomertxt.setAttribute("type", "text");

    var delBtn = document.createElement("input");
    delBtn.setAttribute("type", "button");
    delBtn.setAttribute("value", "Delete");
    delBtn.setAttribute("id", "btnDelete");

    var newArticle = document.createElement("article");
    newArticle.appendChild(newCustomerlbl);
    newArticle.appendChild(newCustomertxt);
    newArticle.appendChild(delBtn);

    var customerSection = document.getElementById("customerRecords");

    var customerArticles = customerSection.getElementsByTagName("article");

    for (var i = 0; i < customerArticles.length; i++) {

        var lastDigit = i + 1;

        var newArticleValue = "article" + lastDigit;
        newArticle.setAttribute("id", newArticleValue);
    }

    customerSection.appendChild(newArticle);
}

Now what I want is whenever user click upon the newly created appended delete button, only that particular article get deleted without effecting the rest of articles. 现在我想要的是每当用户点击新创建的附加删除按钮时,只删除该特定文章而不影响其余文章。

Here is the my jsFiddle code. 这是我的jsFiddle代码。

You need to bind an event listener on the newly created delete button. 您需要在新创建的删除按钮上绑定事件侦听器。 Your example code about using $(this) suggest that you are using JQuery, but then again in the rest of the code you are not using any JQuery? 你使用$(this)示例代码表明你正在使用JQuery,但是在你没有使用任何JQuery的其余代码中再次使用它?

If you are using JQuery, things get real simple, just add something like 如果您正在使用JQuery,事情变得非常简单,只需添加类似的东西

$(document).on('click','.btnDelete', function(){
   $(this).closest('article').remove();
});

(and remember to give the deletebutton a CLASS rather than ID, as there will be multiple delete buttons). (并且记得给deletebutton一个CLASS而不是ID,因为会有多个删除按钮)。

If you are NOT using JQuery, you need to add the event listener EVERY TIME a new delete button is created 如果您不使用JQuery,则需要在每次创建新的删除按钮时添加事件侦听器

newArticle.appendChild(delBtn);
delBtn.onclick = function(.....

etc. 等等

If you don't want to use jQuery you can add event listeners to your buttons: 如果您不想使用jQuery,可以向按钮添加事件侦听器:

delBtn.addEventListener('click', function () {
    this.parentElement.remove();
}, false);

https://jsfiddle.net/3nq1v5e1/ https://jsfiddle.net/3nq1v5e1/

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

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