简体   繁体   English

如何删除div中的最后一个html行?

[英]How do I remove the whole last html line in div?

What I basically am trying to accomplish is a form, where one can add a whole html line dynamically using javascript using one button, or remove an existing line using another. 我基本上想完成的是一种表单,其中可以使用一个按钮使用javascript动态添加整个html行,或使用另一个按钮删除现有的行。 I got the add function to work, yet I cannot seem to figure out the remove function. 我可以使用添加功能,但是似乎无法弄清删除功能。

Here is my code: 这是我的代码:

 window.onload = function(){ var addHw = document.getElementById("addhw"); var removeHw = document.getElementById("removehw"); // Here is my add function addHw.addEventListener('click', function () { var homeworkGrade = document.createElement('input'); homeworkGrade.className = 'grade'; homeworkGrade.type = 'text'; homeworkGrade.size = 3; var overallGrade = document.createElement('homework'); overallGrade.className = 'homework'; overallGrade.type = 'text'; overallGrade.size = 3; var form = document.getElementById("assignments"); var r = "HW <input class=\\"grade\\" type = \\"text \\"size=\\"3 \\">/<input class=\\"homework \\" type = \\"text \\" size= \\"3 \\"><br />"; form.insertAdjacentHTML('beforeend',r); }); // Here is my attempt at the remove function: removeHw.addEventListener('click', function () { var form = document.getElementById("assignments").lastChild; var hw = document.getElementById("homework"); var grade = document.getElementById("grade"); }); } 
 <form id="myForm"> <div id="assignments"> <!-- add HWs here --> HW <input class="grade" type="text" size="3">/<input class="homework" type="text" size="3"><br /> HW <input class="grade" type = "text" size="3 ">/<input class="homework " type = "text " size= "3 "><br /> HW <input class="grade" type = "text "size="3 ">/<input class="homework " type = "text " size= "3 "><br /> </div> <div> <!-- add curve here --> <input type="checkbox" name="curve" />Curve + 5? </div> <div id="resultsarea "> <p> <!--add buttons--> <button type="button" id="compute">Compute!</button> <button type="button" id="addhw">Add HW</button> <button type="button" id="removehw">Remove HW</button> <button type="button" id="clear">Clear</button> </p> <!-- add the results here --> <div id="result"></div> </div> </form> 

I tried the removeChild and tried to remove the last child of "assignments", with no luck. 我尝试了removeChild并尝试删除了“ assignments”的最后一个孩子,但是没有运气。 If someone would like to comment on my code and if it's efficient or provide me some comments that would benefit my progress, I'll be the most thankful. 如果有人想对我的代码发表评论,或者它有效率,或者为我提供一些有益于我的进步的评论,我将非常感谢。

By far the easiest way to do it is to update your code so that your "HW" are wrapped (eg in a span ), and give all of these spans a class (eg "hw"). 到目前为止,最简单的方法是更新代码,以便将“ HW”包装起来(例如,在span ),并为所有这些span提供一个类(例如,“ hw”)。 If you want them to be in different lines anyway, you may as well use a p or a div and remove the <br /> . 如果仍然希望它们位于不同的行中,则最好使用pdiv并删除<br />

 window.addEventListener('load', function(){ var addHw = document.getElementById('addhw'); var removeHw = document.getElementById('removehw'); var hwHTML = '<div class="hw">HW <input class="grade" type="text" size="3" />/<input class="homework" type="text" size="3" /></div>'; var form = document.getElementById("assignments"); // Add hw. addHw.addEventListener('click', function () { // A lot of core were useless here as you only // use the string at the end (and it is sufficient). form.insertAdjacentHTML('beforeend', hwHTML); }); // Remove hw. removeHw.addEventListener('click', function () { form.removeChild(form.querySelector(".hw:last-child")); }); }); 
 <form id="myForm"> <div id="assignments"> <!-- add HWs here --> <div class="hw">HW <input class="grade" type="text" size="3" />/<input class="homework" type="text" size="3" /></div> <div class="hw">HW <input class="grade" type="text" size="3" />/<input class="homework" type="text" size="3" /></div> <div class="hw">HW <input class="grade" type="text" size="3" />/<input class="homework" type="text" size="3" /></div> </div> <div> <!-- add curve here --> <input type="checkbox" name="curve" />Curve + 5? </div> <div id="resultsarea "> <p> <!--add buttons--> <button type="button" id="compute">Compute!</button> <button type="button" id="addhw">Add HW</button> <button type="button" id="removehw">Remove HW</button> <button type="button" id="clear">Clear</button> </p> <!-- add the results here --> <div id="result"></div> </div> </form> 

It would be great to place every HW into its container. 将每个硬件放入其容器中将是很棒的。 Because removal of the whole container is much easier. 因为移除整个容器要容易得多。

Javascript: 使用Javascript:

(function(){
  var addHw = document.getElementById("addhw");
  var removeHw = document.getElementById("removehw");

  // Here is my add function
  addHw.addEventListener('click', function () {
    var form = document.getElementById("assignments");
    var r = "<div>HW <input class=\"grade\"  type = \"text \"size=\"3 \">/<input class=\"homework \" type = \"text \" size= \"3 \"></div>";
    form.insertAdjacentHTML('beforeend',r);
  });

  // Here is my attempt at the remove function:

  removeHw.addEventListener('click', function () {
    var form = document.getElementById("assignments");
    var lastHW = form.lastChild;
      if(lastHW) {
          form.removeChild(lastHW);
      }
  });
})();

Html: HTML:

...
  <div id="assignments">
    <!-- add HWs here -->
    <div>HW <input class="grade" type="text" size="3">/<input class="homework" type="text" size="3"></div>
    <div>HW <input class="grade" type = "text" size="3 ">/<input class="homework " type = "text " size= "3 "></div>
    <div>HW <input class="grade"  type = "text "size="3 ">/<input class="homework " type = "text " size= "3 "></div>
  </div>
...

Example: https://jsfiddle.net/61ytuoyb/ 示例: https//jsfiddle.net/61ytuoyb/

Can you try wrapping the individual assignments in an assignment and add a unique identifier to each assignment ? 您可以尝试将单个作业包装在作业中,并为每个作业添加唯一的标识符吗?

<div id="assignments">
    <div id="assignment_1">HW etc ...</div>
    <div id="assignment_2">HW etc ...</div>
    <div id="assignment_3">HW etc ...</div>
</div>

Use like a global counter variable to create the unique identifier for each assignment. 像全局计数器变量一样使用,可以为每个分配创建唯一的标识符。

Then use the javascript 然后使用javascript

var idToDelete;

addHw.addEventListener('click', function () {
     idToDelete = this.id; //not sure this is how to obtain the id in pure js.
});

removeHw.addEventListener('click', function () {
    var parent = document.getElementById("assignments");
    var child = document.getElementById("assignment_" + idToDelete);
    parent.removeChild(child);
});

This off the top of my head. 这离开了我的头顶。 Untested code. 未经测试的代码。

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

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