现在遇到jQuery选择器我要删除以下范围

时间:2017-05-16 08:42:58

标签: javascript jquery html

我已经能够使用jQuery删除“Departments”这个词,但我现在要删除其中一个“>”在某些

中的HTML代码中留下的字符
<span> > </span>

在哪里发出“Home&gt;&gt; Dog”我想删除其中一个“&gt;”

我不确定如何让jQuery忽略或删除选择器上面的两行HTML?

$(function() {
  $("a[title*='Categories']").hide();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<div class="sitemappath">
  <span><a href="#" title="Home">Home</a></span><span> &gt; </span><span><a href="/allcategories.aspx" title="Categories">Departments</a></span><span> &gt; </span><span><a href="/dog/g7.aspx" title="Dog">Dog</a></span><span> &gt; </span><span><a href="/dog/food/g8.aspx" title="Food">Food</a></span>
</div>

4 个答案:

答案 0 :(得分:2)

将选择器更改为目标父SPAN元素,然后使用.next()在兄弟之后立即定位。使用.addBack()

&#13;
&#13;
$(function() {
  $("span:has(a[title*='Categories'])").next('span').addBack().hide();
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<div class="sitemappath">
  <span><a href="#" title="Home">Home</a></span><span> &gt; </span><span><a href="/allcategories.aspx" title="Categories">Departments</a></span><span> &gt; </span><span><a href="/dog/g7.aspx" title="Dog">Dog</a></span><span> &gt; </span><span><a href="http://dou9070.e-retailer.co.nz/dog/food/g8.aspx" title="Food">Food</a></span>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

.hide()之后,您可以添加.parent().next("span").hide()。它将删除a[title*='Categories']

后的跨度

&#13;
&#13;
$(function() {
  $("a[title*='Categories']").hide().parent().next("span").hide();
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<div class="sitemappath">
  <span><a href="#" title="Home">Home</a></span><span> &gt; </span><span><a href="/allcategories.aspx" title="Categories">Departments</a></span><span> &gt; </span><span><a href="/dog/g7.aspx" title="Dog">Dog</a></span><span> &gt; </span><span><a href="http://dou9070.e-retailer.co.nz/dog/food/g8.aspx" title="Food">Food</a></span>
</div>
&#13;
&#13;
&#13;

答案 2 :(得分:1)

将此添加为$(function(){中的第一行,它会起作用。

$("a[title*='Categories']").parent().next().hide();

答案 3 :(得分:0)

您可以选择span a和下一个span元素,然后使用隐藏它们。

 $("a[title*='Categories']").parent().next('span').addBack().hide();

$("a[title*='Categories']").parent().next('span').addBack().hide();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="sitemappath">
  <span><a href="#" title="Home">Home</a></span><span> &gt; </span><span><a href="/allcategories.aspx" title="Categories">Departments</a></span><span> &gt; </span><span><a href="/dog/g7.aspx" title="Dog">Dog</a></span><span> &gt; </span><span><a href="http://dou9070.e-retailer.co.nz/dog/food/g8.aspx" title="Food">Food</a></span>
</div>

或者您可以使用:has()选择器选择span具有指定属性的a元素。

$("span:has(a[title*='Categories'])").next('span').addBack().hide();

$("span:has(a[title*='Categories'])").next('span').addBack().hide();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="sitemappath">
  <span><a href="#" title="Home">Home</a></span><span> &gt; </span><span><a href="/allcategories.aspx" title="Categories">Departments</a></span><span> &gt; </span><span><a href="/dog/g7.aspx" title="Dog">Dog</a></span><span> &gt; </span><span><a href="http://dou9070.e-retailer.co.nz/dog/food/g8.aspx" title="Food">Food</a></span>
</div>