jquery删除重复的链接

时间:2017-11-16 21:01:03

标签: jquery

我有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&amp;RC=">Other</a></li>
  <li><a href="https://myanalytix.beta.com/Pages/test2.aspx?BF=supply%20chain&amp;RC=Inventory">Inventory</a></li>
  <li><a href="https://myanalytix.beta.com/Pages/test2.aspx?BF=supply%20chain&amp;RC=Manufacturing">Manufacturing</a></li>
  <li><a href="https://myanalytix.beta.com/Pages/test2.aspx?BF=supply%20chain&amp;RC=Metrics">Metrics</a></li>
  <li><a href="https://myanalytix.beta.com/Pages/test2.aspx?BF=supply%20chain&amp;RC=Operational">Operational</a></li>
  <li><a href="https://myanalytix.beta.com/Pages/test2.aspx?BF=supply%20chain&amp;RC=Planning">Planning</a></li>
  <li><a href="https://myanalytix.beta.com/Pages/test2.aspx?BF=supply%20chain&amp;RC=Procurement">Procurement</a></li>
  <li><a href="https://myanalytix.beta.com/Pages/test2.aspx?BF=supply%20chain&amp;RC=Sales Order">Sales Order</a></li>
  <li><a href="https://myanalytix.beta.com/Pages/test2.aspx?BF=supply%20chain&amp;RC=">Other</a></li>
</div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

在您的代码中,请使用正确的选择器:

&#13;
&#13;
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&amp;RC=">Other</a></li>
  <li><a href="https://myanalytix.beta.com/Pages/test2.aspx?BF=supply%20chain&amp;RC=Inventory">Inventory</a></li>
  <li><a href="https://myanalytix.beta.com/Pages/test2.aspx?BF=supply%20chain&amp;RC=Manufacturing">Manufacturing</a></li>
  <li><a href="https://myanalytix.beta.com/Pages/test2.aspx?BF=supply%20chain&amp;RC=Metrics">Metrics</a></li>
  <li><a href="https://myanalytix.beta.com/Pages/test2.aspx?BF=supply%20chain&amp;RC=Operational">Operational</a></li>
  <li><a href="https://myanalytix.beta.com/Pages/test2.aspx?BF=supply%20chain&amp;RC=Planning">Planning</a></li>
  <li><a href="https://myanalytix.beta.com/Pages/test2.aspx?BF=supply%20chain&amp;RC=Procurement">Procurement</a></li>
  <li><a href="https://myanalytix.beta.com/Pages/test2.aspx?BF=supply%20chain&amp;RC=Sales Order">Sales Order</a></li>
  <li><a href="https://myanalytix.beta.com/Pages/test2.aspx?BF=supply%20chain&amp;RC=">Other</a></li>
</ul>
&#13;
&#13;
&#13;

此外,您不能直接在<li><div>。由于您拥有<li>中的所有内容,我建议您删除整个标记:

&#13;
&#13;
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&amp;RC=">Other</a></li>
  <li><a href="https://myanalytix.beta.com/Pages/test2.aspx?BF=supply%20chain&amp;RC=Inventory">Inventory</a></li>
  <li><a href="https://myanalytix.beta.com/Pages/test2.aspx?BF=supply%20chain&amp;RC=Manufacturing">Manufacturing</a></li>
  <li><a href="https://myanalytix.beta.com/Pages/test2.aspx?BF=supply%20chain&amp;RC=Metrics">Metrics</a></li>
  <li><a href="https://myanalytix.beta.com/Pages/test2.aspx?BF=supply%20chain&amp;RC=Operational">Operational</a></li>
  <li><a href="https://myanalytix.beta.com/Pages/test2.aspx?BF=supply%20chain&amp;RC=Planning">Planning</a></li>
  <li><a href="https://myanalytix.beta.com/Pages/test2.aspx?BF=supply%20chain&amp;RC=Procurement">Procurement</a></li>
  <li><a href="https://myanalytix.beta.com/Pages/test2.aspx?BF=supply%20chain&amp;RC=Sales Order">Sales Order</a></li>
  <li><a href="https://myanalytix.beta.com/Pages/test2.aspx?BF=supply%20chain&amp;RC=">Other</a></li>
</ul>
&#13;
&#13;
&#13;