简体   繁体   English

添加/删除子元素

[英]Add/remove child elements

I am using javascript and jQuery to dynamically add and delete new elements to and from an existing <div> . 我正在使用javascript和jQuery在现有的<div>动态添加和删除新元素。

Adding a 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. 添加一个新元素可以正常工作,但是当我单击“删除”以删除该元素时,我得到了Main Container对象,并且按钮内的子元素对象也存在并被单击。

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. 现在,当我尝试删除该元素时,问题就来了,直到包含该控件的html表格控件都在获取该对象,但没有获得包含该表的div。

Note : the div contains the table is the child div/element. 注意:div包含的表是子div / element。

Javascript Java脚本

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");   
  newElement.innerHTML = ab;
  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);
}

The problem occurs with mySelf control that is not accessing the div control that contains the table, due to that delete operation doesn't work. 由于无法执行删除操作,无法访问包含该表的div控件的mySelf控件会出现问题。

Html Code HTML代码

<div id="contControls">
  <div class="optionPane">
    <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>
  </div>
</div>

I'm not able to get the <div> element having class optionPane ; 我无法获得具有类optionPane<div>元素; whenever I try to target the parent of the <table> I get the <div> with the id contControls . 每当我尝试定位<table>的父对象时,我都会得到ID为contControls<div>

Working Demo 工作演示

If you're going through the trouble of importing jQuery, you may as well use it. 如果您在导入jQuery时遇到麻烦,则不妨使用它。 It simplifies all of the DOM manipulation that you are trying to accomplish, and your code boils down to simply this ( note that javascript string can't span multiple lines like you are doing without using + for concatenation ): 它简化了您要完成的所有DOM操作,您的代码可以简化为以下内容( 请注意,如果不使用+进行串联,则javascript字符串无法像您所做的那样跨越多行 ):

var ab = '<table rules="none" width="100%">'
 + '<tr><td class="optHdBg"><a>'
 + '<img src="themes/theme_blog/images/icons/delete.png" name="delete" />'
 + '</a></td></tr></table>';

$(document).ready(function() {
    $(document).on('click', 'img[name="delete"]', function() {
        remove_block(this);
    });        
});

function add() {
    $("<div class='optionPane'></div>").html( ab ).appendTo("#contControls");
}

function remove_block(obj) {
    // remove the entire optionPane <div>
    $(obj).closest('div.optionPane').remove();
}​

Also, you should note that in the line $(document).on('click' you can ( and should ) replace document with whichever static parent element you are adding the dynamic elements to (probably #contControls , but I can't see your HTML); this provides better performance, because events don't have to bubble as far up the DOM. 另外,您应该注意,在$(document).on('click'您可以( 并且应该 )将要添加动态元素的静态父元素替换为document (可能是#contControls ,但我看不到)您的HTML);这样可以提供更好的性能,因为事件不必冒泡到DOM的尽头。

Try this jquery code 试试这个jQuery代码

If you have more than 1 parent node then add after obj 如果父节点超过1个,则在obj之后添加

$(obj).parent().remove();

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

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