使用jquery删除重复的相邻td元素

时间:2016-09-10 08:09:05

标签: javascript jquery html html-table

我的html代码如下所示。

<div class="serverSet">
  <h2>JH Storefront servers</h2>
  <table border="1" class="CSSTableGenerator" class="myTable">
    <tr>
      <th>Component</th>
      <th>Properties</th>
      <th class="servers"> lqwasc10 </th>
      <th class="servers"> lqwasc11 </th>
    </tr>
    <tr>
      <td class="comps">DeliveryMethodsRepository</td>
      <td class="props">externalCacheBatchInfoSize</td>
    <tr/>
    <tr>
      <td class="comps">InventoryManager</td>
      <td class="comps">InventoryManager</td>
      <td class="props">itemType</td>
    <tr/>
    <tr>
      <td class="comps">InventoryManager</td>
      <td class="props">maxConcurrentUpdateRetries</td>
    <tr/>
    <tr>
      <td class="comps">CatalogTools</td>
      <td class="comps">CatalogTools</td>
      <td class="props">queryASAFFabrics</td>
    <tr/>
    <tr>
      <td class="comps">CatalogTools</td>
      <td class="props">loggingDebug</td>
    <tr/>
    <tr>
      <td class="comps">CatalogTools</td>
      <td class="props">outOfStockCode</td>
    </tr> 
  </table>
</div>

在上面的html代码中,相邻属性列中存在的重复组件很少。有没有办法可以从属性列中识别出那些重复的组件并删除它们?

在上面的示例中,属性列中包含两个组件CatalogToolsInventoryManager。因此,它们各自的属性已移至右侧的相邻列。

上面的html代码可能看起来有问题,因为它是从服务器生成的,所以我想整理jquery。

最终,我正在寻找一个html,如截图所示。

Expected table data

如果您需要更多详情,请告诉我。

提前致谢。

1 个答案:

答案 0 :(得分:4)

var dups = $('.comps + .comps')
dups.remove()
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="serverSet">
  <h2>JH Storefront servers</h2>
  <table border="1" class="CSSTableGenerator" class="myTable">
    <tr>
      <th>Component</th>
      <th>Properties</th>
      <th class="servers"> lqwasc10 </th>
      <th class="servers"> lqwasc11 </th>
    </tr>
    <tr>
      <td class="comps">DeliveryMethodsRepository</td>
      <td class="props">externalCacheBatchInfoSize</td>
      <tr/>
      <tr/>
      <td class="comps">InventoryManager</td>
      <td class="comps">InventoryManager</td>
      <td class="props">itemType</td>
      <tr/>
      <td class="comps">InventoryManager</td>
      <td class="props">maxConcurrentUpdateRetries</td>
      <tr/>
      <tr/>
      <td class="comps">CatalogTools</td>
      <td class="comps">CatalogTools</td>
      <td class="props">queryASAFFabrics</td>
      <tr/>
      <td class="comps">CatalogTools</td>
      <td class="props">loggingDebug</td>
      <tr/>
      <td class="comps">CatalogTools</td>
      <td class="props">outOfStockCode</td>
      <tr/>
      <tr/>
    </tr>
    </tr>
  </table>
</div>