无法让.removeChild在新动态创建的div上工作

时间:2016-09-30 13:13:23

标签: javascript

我正在尝试为动态创建的输入框创建一个删除按钮,但我似乎无法让它工作。我感觉到它是因为最初加载脚本时“孩子”不存在,但我不确定。

任何帮助将不胜感激。感谢。

var addButton = document.getElementById("add-button");
var removeButton = document.getElementById("remove-button");
var incomeSection = document.getElementById("income");
var incomeRow = document.getElementById("income-row");
var itemCounter = 2

incomeSection.addEventListener("click", activateItem);

function activateItem(e) {
  if (e.target.id == "add-button") {
    incomeSection.innerHTML += '<div id="income-row"><input class="input" type="text" name="income-type-1" placeholder="Income source ' + itemCounter + '"><span class="currency">£</span><input class="input income-amount" type="text" name="income-amount-1" placeholder="Amount"><input id="remove-button" class="button" type="button" value="-"></div>';
    itemCounter++;
  }

  if (e.target.id == "remove-button") {
    incomeSection.removeChild(incomeRow);
  }
}
html body {
  background-color: #c9c9c9;
  font-family: 'Montserrat', sans-serif;
  color: #686868;
}
#income {
  padding: 10px;
}
.input {
  padding: 8px;
  font-size: 14px;
  margin-right: 10px;
  margin-bottom: 5px;
  width: 30%;
}
.button {
  width: 25px;
  height: 25px;
}
.currency {
  padding: 10px;
  font-size: 14px;
  margin-right: -4px;
  margin-bottom: 5px;
  background-color: darkgray;
  color: white;
}
<div id="income">
  <div id="income-displayed">
    <h2>Income: <span></span></h2>
  </div>
  <div id="income-row">
    <input class="input" type="text" name="income-type-1" placeholder="Income source">
    <span class="currency">£</span>
    <input class="input income-amount" type="text" name="income-amount-1" placeholder="Amount">
    <input id="add-button" class="button" type="button" value="+">
  </div>
</div>

2 个答案:

答案 0 :(得分:0)

只需更换以下行即可解决您的问题: -

incomeSection.removeChild(incomeRow);

以下

e.target.parentNode.remove();

&#13;
&#13;
var addButton = document.getElementById("add-button");
var removeButton = document.getElementById("remove-button");
var incomeSection = document.getElementById("income");
var incomeRow = document.getElementById("income-row");
var itemCounter = 2

incomeSection.addEventListener("click", activateItem);

function activateItem(e) {
    if (e.target.id == "add-button") {
        incomeSection.innerHTML += '<div id="income-row"><input class="input" type="text" name="income-type-1" placeholder="Income source '+itemCounter+'"><span class="currency">£</span><input class="input income-amount" type="text" name="income-amount-1" placeholder="Amount"><input id="remove-button" class="button" type="button" value="-"></div>'; itemCounter ++;
    }
    
    if (e.target.id == "remove-button") {
        e.target.parentNode.remove();
    }
}
&#13;
html body {
  background-color: #c9c9c9;
  font-family: 'Montserrat', sans-serif;
  color: #686868;
}
#income {
  padding: 10px;
}
.input {
  padding: 8px;
  font-size: 14px;
  margin-right: 10px;
  margin-bottom: 5px;
  width: 30%;
}
.button {
  width: 25px;
  height: 25px;
}
.currency {
  padding: 10px;
  font-size: 14px;
  margin-right: -4px;
  margin-bottom: 5px;
  background-color: darkgray;
  color: white;
}
&#13;
<div id="income">
  <div id="income-displayed"><h2>Income: <span></span></h2></div>
  <div id="income-row">
    <input class="input" type="text" name="income-type-1" placeholder="Income source"><span class="currency">£</span><input class="input income-amount" type="text" name="income-amount-1" placeholder="Amount"><input id="add-button" class="button" type="button" value="+"></div>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

此代码段演示了如何使用以下方法在DOM中添加和删除元素:

更多细节在来源

中有所评论

// #incFormA element will bind with eventListener
var form = document.getElementById("incFormA");
// #setA will contain the #dupes
var set = document.getElementById("setA");
// Reference to clone template #dupe
var dupe = document.getElementById('dupe');
// Increment counter that will ensure unique ids
var cntr = 0;
// If any part of the form is clicked call actDeact
form.addEventListener("click", actDeact);

/*
| actDeact()
| -If the clicked button's id === addA
| then clone #dupe and add an id (#dupe+cntr)
| -Iterate through it's children
| and assign unique ids and names to 
| the 1st and 3rd children of (#dupe+cntr)
| -Append #dupe+cntr to #setA
*/
/*
| -If the clicked button was a .remA
| find it's parentNode and remove it.
*/
function actDeact(e) {
  cntr++;
  if (e.target.id === 'addA') {

    var clone = dupe.cloneNode(true);
    clone.id = 'dupe' + cntr;
    var i;
    var grp = clone.children;
    var qty = grp.length;

    for (i = 0; i < qty; i++) {
      switch (i) {
        case 0:
          grp[0].id = 'inc' + cntr;
          grp[0].name = 'inc' + cntr;
          break;
        case 2:
          grp[2].id = 'amt' + cntr;
          grp[2].name = 'amt' + cntr;
          break;
        default:
          break;
      }

    }
    setA.appendChild(clone);

  } else if (e.target.className === "remA") {

    var clone = e.target.parentNode;
    setA.removeChild(clone);

  } else return false;
}
html body {
  background-color: #c9c9c9;
  font-family: 'Montserrat', sans-serif;
  color: #686868;
}

#setA {
  padding: 10px;
}


/* The clone template is out of the way 
| yet still accessible for cloning.
*/

#dupe {
  display: none;
}

.input {
  padding: 8px;
  font-size: 14px;
  margin-right: 10px;
  margin-bottom: 5px;
  width: 30%;
}

button {
  width: 25px;
  height: 25px;
  float: right;
}

.currency {
  padding: 10px;
  font-size: 14px;
  margin-right: -4px;
  margin-bottom: 5px;
  background-color: darkgray;
  color: white;
}


/*
| This ruleset is for positioning #addA
*/

.block {
  position: relative;
  display: block;
  min-height: 100%;
  width: 20%;
  left: 80%;
  bottom: 15px;
}
<!--Use <form> to post collect and send data*-->
<form id='incFormA' name='incFormA'>
  <fieldset id="setA">

    <legend class="display">
      <!--Use <output> to calculate and display data*-->
      <h2>Income: <output id='outA'></output></h2>
    </legend>
    <div class='block'>
      <!--One add <button>-->
      <button id="addA" type='button'>+</button>
    </div>
    <!--#dupe is the group of elements kept as a clone template-->
    <span id='dupe'>
      <input id='inc' class="input" name="inc" type="text" placeholder="Income source">
      <span class="currency">£</span>
    <input id="amt" class="input" name="amt" type="text" placeholder="Amount">
    <button class="remA">-</button>
    </span>

  </fieldset>
</form>

<!--*Choice of elements optional-->