我有jQuery删除重复的链接,但它删除整个页面的链接。我需要针对特定的div。我只需要从nav-report-category中删除重复的A
var seen = {};
$('a').each(function() {
var txt = $(this).text();
if (seen[txt])
$(this).remove();
else
seen[txt] = true;
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="nav-report-category">
<li><a href="https://myanalytix.beta.com/Pages/test2.aspx?BF=supply%20chain&RC=">Other</a></li>
<li><a href="https://myanalytix.beta.com/Pages/test2.aspx?BF=supply%20chain&RC=Inventory">Inventory</a></li>
<li><a href="https://myanalytix.beta.com/Pages/test2.aspx?BF=supply%20chain&RC=Manufacturing">Manufacturing</a></li>
<li><a href="https://myanalytix.beta.com/Pages/test2.aspx?BF=supply%20chain&RC=Metrics">Metrics</a></li>
<li><a href="https://myanalytix.beta.com/Pages/test2.aspx?BF=supply%20chain&RC=Operational">Operational</a></li>
<li><a href="https://myanalytix.beta.com/Pages/test2.aspx?BF=supply%20chain&RC=Planning">Planning</a></li>
<li><a href="https://myanalytix.beta.com/Pages/test2.aspx?BF=supply%20chain&RC=Procurement">Procurement</a></li>
<li><a href="https://myanalytix.beta.com/Pages/test2.aspx?BF=supply%20chain&RC=Sales Order">Sales Order</a></li>
<li><a href="https://myanalytix.beta.com/Pages/test2.aspx?BF=supply%20chain&RC=">Other</a></li>
</div>
&#13;
答案 0 :(得分:0)
在您的代码中,请使用正确的选择器:
var seen = {};
$('.nav-report-category a').each(function() {
var txt = $(this).text();
if (seen[txt])
$(this).remove();
else
seen[txt] = true;
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="nav-report-category">
<li><a href="https://myanalytix.beta.com/Pages/test2.aspx?BF=supply%20chain&RC=">Other</a></li>
<li><a href="https://myanalytix.beta.com/Pages/test2.aspx?BF=supply%20chain&RC=Inventory">Inventory</a></li>
<li><a href="https://myanalytix.beta.com/Pages/test2.aspx?BF=supply%20chain&RC=Manufacturing">Manufacturing</a></li>
<li><a href="https://myanalytix.beta.com/Pages/test2.aspx?BF=supply%20chain&RC=Metrics">Metrics</a></li>
<li><a href="https://myanalytix.beta.com/Pages/test2.aspx?BF=supply%20chain&RC=Operational">Operational</a></li>
<li><a href="https://myanalytix.beta.com/Pages/test2.aspx?BF=supply%20chain&RC=Planning">Planning</a></li>
<li><a href="https://myanalytix.beta.com/Pages/test2.aspx?BF=supply%20chain&RC=Procurement">Procurement</a></li>
<li><a href="https://myanalytix.beta.com/Pages/test2.aspx?BF=supply%20chain&RC=Sales Order">Sales Order</a></li>
<li><a href="https://myanalytix.beta.com/Pages/test2.aspx?BF=supply%20chain&RC=">Other</a></li>
</ul>
&#13;
此外,您不能直接在<li>
内<div>
。由于您拥有<li>
中的所有内容,我建议您删除整个标记:
var seen = {};
$('.nav-report-category a').each(function() {
var txt = $(this).text();
if (seen[txt])
$(this).closest("li").remove();
else
seen[txt] = true;
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="nav-report-category">
<li><a href="https://myanalytix.beta.com/Pages/test2.aspx?BF=supply%20chain&RC=">Other</a></li>
<li><a href="https://myanalytix.beta.com/Pages/test2.aspx?BF=supply%20chain&RC=Inventory">Inventory</a></li>
<li><a href="https://myanalytix.beta.com/Pages/test2.aspx?BF=supply%20chain&RC=Manufacturing">Manufacturing</a></li>
<li><a href="https://myanalytix.beta.com/Pages/test2.aspx?BF=supply%20chain&RC=Metrics">Metrics</a></li>
<li><a href="https://myanalytix.beta.com/Pages/test2.aspx?BF=supply%20chain&RC=Operational">Operational</a></li>
<li><a href="https://myanalytix.beta.com/Pages/test2.aspx?BF=supply%20chain&RC=Planning">Planning</a></li>
<li><a href="https://myanalytix.beta.com/Pages/test2.aspx?BF=supply%20chain&RC=Procurement">Procurement</a></li>
<li><a href="https://myanalytix.beta.com/Pages/test2.aspx?BF=supply%20chain&RC=Sales Order">Sales Order</a></li>
<li><a href="https://myanalytix.beta.com/Pages/test2.aspx?BF=supply%20chain&RC=">Other</a></li>
</ul>
&#13;