首页 > 解决方案 > 使用 JQuery 删除重复的 div

问题描述

我有以下结构:

<div class="section-header Generator" style="display: table-row;">Generator</div>
<div class="section-header Generator" style="display: table-row;">Generator</div>
<div class="section-header Generator" style="display: table-row;">Generator</div>
<div class="section-header Engine" style="display: table-row;">Engine</div>
<div class="section-header Engine" style="display: table-row;">Engine</div>
<div class="section-header Battery" style="display: table-row;">Battery</div>

使用 JQuery,我想删除重复项 - 所以上面的列表将变为:

<div class="section-header Generator" style="display: table-row;">Generator</div>
<div class="section-header Engine" style="display: table-row;">Engine</div>
<div class="section-header Battery" style="display: table-row;">Battery</div>

任何想法我将如何实现?这里有一个旧答案,但似乎与我的问题无关

标签: jquery

解决方案


解决方案在这里 - 我意识到我使用了错误的搜索词:

var seen = {};
$('.table-section-header').each(function() {
    var txt = $(this).text();
    if (seen[txt])
        $(this).remove();
    else
        seen[txt] = true;
});

推荐阅读