如何使用id删除div中生成的表?

问题描述 投票:0回答:1

我创建了一个不可见的div,在该div中主表是隐藏的,我把该表克隆到另一个div中。 该div 身份证main-div.

我想用它的表id从该div中删除新生成的表,有什么提示吗?

var aggTableNum = 0;
$('.addAggregatorCompo').click(function (e) {
  const originTable = document.getElementById('aggregator-table');
   let newTable = originTable.cloneNode(true);
   newTable.id = 'newAggTable' + ++aggTableNum;
   newTable.querySelectorAll('input').forEach((element) => {
    element.value = '';
  });
   $('#main-div').append(newTable);
  //attach();
});

// write delete table using its id
$('.component-base, .generate-div').on(
  'click',
  '.delete-component',
  function (e) {
    console.log(e.delegateTarget.id);
 }
);
#aggregator-table {
  display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button style="margin: 1%" class="addAggregatorCompo">Add Table</button>
<table id="aggregator-table" class="component-base">
        <thead>
          
        <th colspan="6">Aggregator</th>
    
        <tr>
          <th> Column Name </th>
          <th> Function </th>
          <th> Alias </th>
          <th> Order </th>
      
        </tr>
        </thead>
        <tbody>
        <tr>
      
          <td>
            <input>
            </input>
          </td>
          <td><input>
            </input></td>
          <td>
            <input>
            </input>
          </td>
            <td>
              <input>
              </input>
            </td>
        </tr>
        <tr>
          <td>
        <button style="margin: 1%" onclick="addAgg(this,event)">add Properties</button>
        <!-- <button style="margin: 1%">add Properties</button> -->
        </td>
        <td>
          <button class="delete-component" style="margin: 1%">delete </button>
        </td>
      </tr>
      </tbody>
      </table>
      <div class="generate-div" id="main-div"></div>

JsFiddle Linke:-> https:/jsfiddle.netshreekantbatale21w08ksfa2。

javascript html jquery dom
1个回答
2
投票

委托点击并找到最接近的表


$('#main-div').on("click", ".delete-component",function(e) {
  e.preventDefault(); // in case it is not a type=button and the table is wrapped in a form
  this.closest("table").remove();
})

var aggTableNum = 0;
$('.addAggregatorCompo').click(function(e) {
  const originTable = document.getElementById('aggregator-table');
  let newTable = originTable.cloneNode(true);
  newTable.id = 'newAggTable' + ++aggTableNum;
  newTable.querySelectorAll('input').forEach((element) => {
    element.value = '';
  });
  $('#main-div').append(newTable);
  //attach();
});

$('#main-div').on("click", ".delete-component", function(e) {
  this.closest("table").remove();
})


// write delete table using its id
$('.component-base, .generate-div').on(
  'click',
  '.delete-component',
  function(e) {
    console.log(e.delegateTarget.id);
  }
);
#aggregator-table {
  display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button style="margin: 1%" class="addAggregatorCompo">Add Table</button>
<table id="aggregator-table" class="component-base">
  <thead>

    <th colspan="6">Aggregator</th>

    <tr>
      <th> Column Name </th>
      <th> Function </th>
      <th> Alias </th>
      <th> Order </th>

    </tr>
  </thead>
  <tbody>
    <tr>

      <td>
        <input>
        </input>
      </td>
      <td><input>
        </input>
      </td>
      <td>
        <input>
        </input>
      </td>
      <td>
        <input>
        </input>
      </td>
    </tr>
    <tr>
      <td>
        <button style="margin: 1%" onclick="addAgg(this,event)">add Properties</button>
        <!-- <button style="margin: 1%">add Properties</button> -->
      </td>
      <td>
        <button class="delete-component" style="margin: 1%">delete </button>
      </td>
    </tr>
  </tbody>
</table>
<div class="generate-div" id="main-div"></div>

如果你必须这样做,你可以做

const id = 'newAggTable' + $('#main-div').find("table").length;
$newTable.prop("id",id);
$newTable.find(".delete-component").data("id",id);

而后

$('#main-div').on("click", ".delete-component", function(e) {
  $("#"+$(this).data("id")).remove();
})

var aggTableNum = 0;
$('.addAggregatorCompo').click(function(e) {
  const $originTable = $('#aggregator-table');
  let $newTable = $originTable.clone(true);
  const id = 'newAggTable' + $('#main-div').find("table").length;
  $newTable.prop("id",id)
  $newTable.find('input').each(element => $(element).val(''));
  $('#main-div').append($newTable);
  $newTable.find(".delete-component").data("id",id)
});

$('#main-div').on("click", ".delete-component", function(e) {
  $("#"+$(this).data("id")).remove();
})


// write delete table using its id
$('.component-base, .generate-div').on(
  'click',
  '.delete-component',
  function(e) {
    console.log(e.delegateTarget.id);
  }
);
#aggregator-table {
  display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button style="margin: 1%" class="addAggregatorCompo">Add Table</button>
<table id="aggregator-table" class="component-base">
  <thead>

    <th colspan="6">Aggregator</th>

    <tr>
      <th> Column Name </th>
      <th> Function </th>
      <th> Alias </th>
      <th> Order </th>

    </tr>
  </thead>
  <tbody>
    <tr>

      <td>
        <input>
        </input>
      </td>
      <td><input>
        </input>
      </td>
      <td>
        <input>
        </input>
      </td>
      <td>
        <input>
        </input>
      </td>
    </tr>
    <tr>
      <td>
        <button style="margin: 1%" onclick="addAgg(this,event)">add Properties</button>
        <!-- <button style="margin: 1%">add Properties</button> -->
      </td>
      <td>
        <button class="delete-component" style="margin: 1%">delete </button>
      </td>
    </tr>
  </tbody>
</table>
<div class="generate-div" id="main-div"></div>

1
投票
$(this).closest('table.component-base').remove();

将删除 "组件-基地 "类的父表。

摆弄

© www.soinside.com 2019 - 2024. All rights reserved.