附加关闭按钮以关闭/删除附加的按钮节点 - Javascripts

时间:2018-06-07 10:00:38

标签: javascript html append appendchild

我写了一个附加手风琴元素的脚本。单击该手风琴将显示一些用户输入,但是如果需要,我需要能够删除该手风琴元素。以下是剧本:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1"> 
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="sample_reception_page.css">
<style>
.accordion {
    background-color: #eee;
    color: #444;
    cursor: pointer;
    padding: 18px;
    width: 100%;
    border: none;
    text-align: left;
    outline: none;
    font-size: 15px;
    transition: 0.4s;
}
.active, .accordion:hover {
    background-color: #ccc; 
}
.panel {
    padding: 0 18px;
    display: none;
    background-color: white;
    overflow: hidden;
}

.close {
  float: right;
  padding: 12px 16px 12px 16px;
}
.close:hover {
  background-color: #f44336;
  color: white;
}
</style>
</head>
<body>

<h2>Example</h2>
<button onclick="newSample()">ADD</button>

<div id="samplesinfo">
<button onclick="openBtn()" id="sample" class="accordion">Button 1</button>
<div id="usrInput" class="panel">
  <input type="text" name="test">
</div>    
</div>
<div id="paste">    
</div>

<script>

function openBtn(){ 
    var acc = document.getElementsByClassName("accordion");
    var i;

    for (i = 0; i < acc.length; i++) {
        acc[i].addEventListener("click", function() {
            this.classList.toggle("active");
            var panel = this.nextElementSibling;
            if (panel.style.display === "block") {
                panel.style.display = "none";
            } else {
                panel.style.display = "block";
            }
        });
    }
}
</script>

<script>
    var myNodelist = document.getElementById("sample");
    var i;
    for (i = 0; i < myNodelist.length; i++) {
      var span = document.createElement("BUTTON");
      var txt = document.createTextNode("\u00D7");
      span.className = "close";
      span.appendChild(txt);
      myNodelist[i].appendChild(span);
    }

    var close = document.getElementsByClassName("close");
    var i;
    for (i = 0; i < close.length; i++) {
      close[i].onclick = function() {
        var div = this.parentElement;
        div.style.display = "none";
      }
    }

  function newSample() {        
    var variable = document.getElementById("samplesinfo");
    var copy = variable.cloneNode(true);
    var paste = document.getElementById("paste")
    paste.appendChild(copy);

    var span = document.createElement("BUTTON");
    var txt = document.createTextNode("\u00D7");
    span.className = "close";
    span.appendChild(txt);
    li.appendChild(span);

    for (i = 0; i < close.length; i++) {
      close[i].onclick = function() {
        var div = this.parentElement;
        div.style.display = "none";
      }
    }
  } 
</script>
</body>
</html>

我试图在手风琴按钮上添加一个按钮,该按钮将删除该手风琴元素(不是第一个元素),但是尽管这在列表上工作,但它在这个实例中不起作用。有关如何调整javascript的任何建议吗?

0 个答案:

没有答案