添加和删​​除子元素 [英] add and remove child elements

查看:84
本文介绍了添加和删​​除子元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好朋友,

我正在使用代码中的HTML,javascript和jquery,动态地向现有的"div"中添加和删除新元素.

添加新元素可以正常工作,但是当我单击Delete删除元素时,我得到了Main Container对象,并且按钮内的子元素对象存在并被单击.

现在问题来了,当我尝试删除元素时,我正在获取对象,直到包含控件的html table 控件,但没有获取包含该表的div.

注意:包含表的div是子div/element.

添加元素.

Hello Friends,

I am working with HTML,javascript and jquery in my code, dynamically add and delete the new elements to the existing "div".

Adding new element is working fine, but when I click on delete to remove the element, I get the Main Container object and also the child element object within the button exists and was clicked.

now the problem comes when I try to delete the element, I am getting the object till the html table control that contains the controls, but do not get the div that is containing the table.

Note : the div contains the table is the child div/element.

To Add element.

var ab = ''<table rules="none" width="100%">
<tr>
<td class="optHdBg">
<a><img src="themes/theme_blog/images/icons/delete.png" name="delete"  önclick="javascript:remove_block(this);"/></a>
</td></tr></table>'';

function add()
{
var lstChild = $("#contControls").children().last();
var containerElement = document.getElementById("contControls");	
var newElement = document.createElement("div");
$(newElement).addClass("optionPane");
document.getElementById("contControls").appendChild(newElement);
}

function remove_block(obj)
{
  var mainContainer = $(obj).parents("div #contControls");
  var mySelf = obj.parentNode.parentNode.parentNode.parentNode.parentNode;
  mainContainer.removeChild(mySelf);
}



mySelf 控件发生问题,该控件无法访问包含表的 div 控件,原因是删除操作无效.


请指导我.



problem occurs with mySelf control that is not accessing the div control that contains the table, due to that delete operation doesn''t work.


please guide me.
Thanks.

推荐答案

(#contControls").children().last();谢谢. var containerElement = document.getElementById("contControls"); var newElement = document.createElement("div");
("#contControls").children().last(); var containerElement = document.getElementById("contControls"); var newElement = document.createElement("div");


(newElement).addClass("optionPane"); document.getElementById("contControls").appendChild(newElement); } 函数remove_block(obj) { var mainContainer =
(newElement).addClass("optionPane"); document.getElementById("contControls").appendChild(newElement); } function remove_block(obj) { var mainContainer =


(obj).parents("div #contControls"); var mySelf = obj.parentNode.parentNode.parentNode.parentNode.parentNode; mainContainer.removeChild(mySelf); }
(obj).parents("div #contControls"); var mySelf = obj.parentNode.parentNode.parentNode.parentNode.parentNode; mainContainer.removeChild(mySelf); }



mySelf 控件发生问题,该控件无法访问包含表的 div 控件,原因是删除操作无效.


请指导我.
谢谢.



problem occurs with mySelf control that is not accessing the div control that contains the table, due to that delete operation doesn''t work.


please guide me.
Thanks.


这篇关于添加和删​​除子元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆