[英]How do i remove a dynamically created div?
i have a the following code to load a treeview with data using jstree plugin and plugin was attached to index view of MVC4 我有一个以下代码使用jstree插件加载数据树视图,插件附加到MVC4的索引视图
<div id="jstree_demo_div">
<ul>
<li class="jstree-closed">Root node 1
<ul>
<li class="c1">child1</li>
<li class="c1">child2</li>
</ul>
</li>
<li class="jstree-closed">Root node 2
<ul>
<li class="c1">child3</li>
<li class="c1">child4</li>
</ul>
</li>
</ul>
</div>
<div id="Divtxt1"></div>
<h2>Index</h2>
@section Scripts{
<script src="Scripts/jstree.min.js"></script>
<script>
$(document).ready(function () {//{ "theme": { "icons": true }
$('#jstree_demo_div').jstree();
$(document).on('click', '.c1', function () {
var nodeText = $(this).text();
//alert(nodeText);
$('#Divtxt1').append('<div style="display:inline-block;border:1px solid black" ' + 'id=' + nodeText + '>' +
'<textarea style=width:100px;height:100px;visibility:visible;' + 'id=txta' + nodeText + '>' + nodeText +
':</textarea>')
.append('<input type="button" class="del" value="Del"' + 'id=' +'btn-' + nodeText + ' />')
.append('</div><br />');
});
$(document).on('click', '.del', function () {
var btnId = $(this).attr('id');
var coll = btnId.split('-');
alert(coll[1]);
//alert( $(this).attr('id') )
alert($(this).find('textarea[id=txta'+coll[1]+']').text());
alert( $(this).closest('div').html() );
});
});
</script>
}
the above code will dynamically add divs with <textarea>
and a button when the user clicks a child node
of any parent. 当用户单击任何父
child node
的child node
时,上面的代码将动态添加带有<textarea>
div和一个按钮。 The purpose of the button is to delete its div
( so it will remove the textarea and also button with it). 按钮的目的是删除它的
div
(因此它将删除textarea以及带有它的按钮)。 But the problem i have is the button does not delete it. 但我遇到的问题是按钮不会删除它。
another is the following code: alert($(this).find('textarea[id=txta'+coll[1]+']').text());
另一个是以下代码:
alert($(this).find('textarea[id=txta'+coll[1]+']').text());
should display the text in the textarea but it does not either. 应该在textarea中显示文本,但它也不显示。 So how do i remove the div the button is in?
那么如何删除按钮所在的div?
There are 2 problems, first they way you have used append()
is not proper, it doesn't work like string concatenation. 有两个问题,首先他们使用
append()
方式不正确,它不像字符串连接那样工作。 Second you need to delete the parent div
element 其次,您需要删除父
div
元素
$(document).ready(function() { //{ "theme": { "icons": true } $('#jstree_demo_div').jstree(); $(document).on('click', '.c1', function() { var nodeText = $(this).text(); //alert(nodeText); var html = '<div style="display:inline-block;border:1px solid black" ' + 'id=' + nodeText + '>' + '<textarea style=width:100px;height:100px;visibility:visible;' + 'id=txta' + nodeText + '>' + nodeText + ':</textarea>' + '<input type="button" class="del" value="Del"' + 'id=' + 'btn-' + nodeText + ' /></div><br />'; $('#Divtxt1').append(html); }); $(document).on('click', '.del', function() { $(this).closest('div').remove(); }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.2.1/jstree.min.js"></script> <link href="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.2.1/themes/default/style.min.css" rel="stylesheet" /> <div id="jstree_demo_div"> <ul> <li class="jstree-closed">Root node 1 <ul> <li class="c1">child1</li> <li class="c1">child2</li> </ul> </li> <li class="jstree-closed">Root node 2 <ul> <li class="c1">child3</li> <li class="c1">child4</li> </ul> </li> </ul> </div> <div id="Divtxt1"></div> <h2>Index</h2>
Try this :You can find the parent div using closest()
and then call remove()
method on it to remove the div and its child elements. 试试这个:你可以使用
closest()
找到父div,然后调用remove()
方法来删除div及其子元素。
$(document).on('click', 'input.del', function () {
var $parentDiv = $(this).closest('div');
$parentDiv.remove();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.