我已经能够使用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> > </span><span><a href="/allcategories.aspx" title="Categories">Departments</a></span><span> > </span><span><a href="/dog/g7.aspx" title="Dog">Dog</a></span><span> > </span><span><a href="/dog/food/g8.aspx" title="Food">Food</a></span>
</div>
答案 0 :(得分:2)
将选择器更改为目标父SPAN元素,然后使用.next()
在兄弟之后立即定位。使用.addBack()
$(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> > </span><span><a href="/allcategories.aspx" title="Categories">Departments</a></span><span> > </span><span><a href="/dog/g7.aspx" title="Dog">Dog</a></span><span> > </span><span><a href="http://dou9070.e-retailer.co.nz/dog/food/g8.aspx" title="Food">Food</a></span>
</div>
&#13;
答案 1 :(得分:1)
在.hide()
之后,您可以添加.parent().next("span").hide()
。它将删除a[title*='Categories']
$(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> > </span><span><a href="/allcategories.aspx" title="Categories">Departments</a></span><span> > </span><span><a href="/dog/g7.aspx" title="Dog">Dog</a></span><span> > </span><span><a href="http://dou9070.e-retailer.co.nz/dog/food/g8.aspx" title="Food">Food</a></span>
</div>
&#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> > </span><span><a href="/allcategories.aspx" title="Categories">Departments</a></span><span> > </span><span><a href="/dog/g7.aspx" title="Dog">Dog</a></span><span> > </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> > </span><span><a href="/allcategories.aspx" title="Categories">Departments</a></span><span> > </span><span><a href="/dog/g7.aspx" title="Dog">Dog</a></span><span> > </span><span><a href="http://dou9070.e-retailer.co.nz/dog/food/g8.aspx" title="Food">Food</a></span>
</div>