【问题标题】:How to delete generated table from div using its id?如何使用其 id 从 div 中删除生成的表?
【发布时间】:2020-04-27 16:26:20
【问题描述】:

我创建了一个不可见的 div,该 div 主表被隐藏,我正在将该表克隆到另一个 div 中。那个 div idmain-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.net/shreekantbatale2/1w08ksfa/2/

【问题讨论】:

    标签: javascript html jquery dom


    【解决方案1】:

    委托点击并查找最近的表


    $('#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>

    【讨论】:

    • 每当我添加一个新表然后为其分配新的 id 时,我的表 id 都是唯一的
    • @ShreeBatale 当然可以。只要删除按钮在您要删除的表内,就无需考虑。但是一分钟后看到我的更新
    • 当然!我想使用它的 id 删除一个表。
    • 请查看更新。我在第二个示例中使用 ID - 我从 div 中的表数创建 ID,而不是添加到事物中
    • Nvm,它使用的是数据 id,而不是实际的 id。 XD。无论如何,在这个用例中强制使用 id 似乎仍然是一种代码味道。
    【解决方案2】:
    $(this).closest('table.component-base').remove();
    

    将删除类为“component-base”的父表。

    Fiddle

    【讨论】:

    • 我已经建议过了。为什么不将代码保存在 SO 中?
    猜你喜欢
    • 2011-06-03
    • 2017-11-27
    • 1970-01-01
    • 2021-07-22
    • 1970-01-01
    • 1970-01-01
    • 2013-11-24
    • 1970-01-01
    相关资源
    最近更新 更多