我正在使用javascript克隆div,并且如果单击添加更多按钮并且div被克隆,我想使关闭按钮可见。原始父项必须隐藏“关闭”按钮,而克隆项必须可见。
我尝试了以下代码:
var i=0;
var original = document.getElementById('clone_div');
function duplicate(){
var clone = original.cloneNode(true);
clone.id="clone_div" + ++i;
original.parentNode.appendChild(clone);
var clonned_close =
document.getElementById(clone.id).getElementsByClassName('close');
clonned_close.classList.remove('hide');
}
<div>
<h5 class="p-1 border-bottom"><strong>Add Members:</strong></h5>
<div id="clone_div">
<h5 class="pt-2 pb-1">Member<span class="ml-auto close hide"
style="cursor: pointer;">x</span></h5>
<div class="row">
<div class="col-lg-4 col-md-6 col-sm-12">
<div class="form-group">
<label>Name <span class="text-danger">*</span>
</label>
<input type="text" name="mem_name[]" class="form-
control w-100">
</div>
</div>
<div class="col-lg-4 col-md-6 col-sm-12">
<div class="form-group">
<label>Designation <span class="text-danger">*
</span></label>
<input type="text" name="designation[]"
class="form-control w-100">
</div>
</div>
<div class="col-lg-4 col-md-6 col-sm-12">
<div class="form-group">
<label>image Upload <span class="text-danger">*
</span></label>
<input type="file" name="image[]" class="form-
control w-100">
</div>
</div>
</div>
</div>
</div>
<a href="#!" title="" class="btn btn-sm btn-green"
onclick="duplicate()"><i class="fas fa-plus pr-3"></i>Add more</a>
控制台中的错误是: 未被捕获的TypeError:无法读取未定义的属性“删除”