[英]Remove content between two span using jquery
Here is my HTML Code 这是我的HTML代码
<div class="breadcrumbs">
<span typeof="v:Breadcrumb">
<a title="Go to Innomations." href="" class="home">Home</a></span>
>>
<span typeof="v:Breadcrumb" class="turnoffmain">
<a rel="v:url" property="v:title" title="Go to Products." href="">Products</a></span>
</div>
I want to remove the content >>
between two span. 我要删除两个跨度之间的内容>>
。
How can i do this ? 我怎样才能做到这一点 ?
I tried like this 我尝试过这样
$("a").each(function(){
if($(this).attr("title") == "Go to Products."){
$(this).closest('span').addClass('turnoffmain');
}
ie, iterated towards a href and identified the "Go to Products."
也就是说,迭代到href并标识了"Go to Products."
and added a class turnoffmain
which will hide the second span. 并添加了一个类turnoffmain
,它将隐藏第二个跨度。
But how can i remove the >>
between those two spans ? 但是,如何删除这两个跨度之间的>>
?
You can do something like this with the help of contents()
and each()
您可以在contents()
和each()
的帮助下做类似的事情
$('.breadcrumbs').contents().each(function() { if (this.nodeType == 3) this.remove(); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div class="breadcrumbs"> <span typeof="v:Breadcrumb"> <a title="Go to Innomations." href="" class="home">Home</a></span> >> <span typeof="v:Breadcrumb" class="turnoffmain"> <a rel="v:url" property="v:title" title="Go to Products." href="">Products</a></span> </div>
span
tag
或者在span
标签之间添加更具体的删除元素
$('.breadcrumbs').contents().each(function() { console.log(this.nextSibling.nodeName == 'SPAN'); if (this.nodeType == 3 && this.nextSibling && this.prevSibling && this.nextSibling.nodeName == 'SPAN' && this.prevSibling.nodeName == 'SPAN') this.remove(); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div class="breadcrumbs"> <span typeof="v:Breadcrumb"> <a title="Go to Innomations." href="" class="home">Home</a></span> >> <span typeof="v:Breadcrumb" class="turnoffmain"> <a rel="v:url" property="v:title" title="Go to Products." href="">Products</a></span> </div>
Or as you said removing content if it is above the class turnoffmain
或者就像您所说的,如果内容超出类turnoffmain
则将其turnoffmain
$('.breadcrumbs').contents().each(function() { console.log(this.nextSibling.nodeName == 'SPAN'); if (this.nodeType == 3 && $(this.nextSibling).is('.turnoffmain')) this.remove(); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div class="breadcrumbs"> <span typeof="v:Breadcrumb"> <a title="Go to Innomations." href="" class="home">Home</a></span> >> <span typeof="v:Breadcrumb" class="turnoffmain"> <a rel="v:url" property="v:title" title="Go to Products." href="">Products</a></span> </div>
var a = '<div class="breadcrumbs"><span typeof="v:Breadcrumb"> <a title="Go to Innomations." href="" class="home">Home</a></span> >> <span typeof="v:Breadcrumb" class="turnoffmain"><a rel="v:url" property="v:title" title="Go to Products."href="">Products</a></span> </div>';
var str = a;
str = str.replace(">>", "");
alert(a)
Basic example; 基本示例;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.