简体   繁体   English

如何<a>使用jQuery</a>替换<a>部分中的</a>最后一个<a>标签</a>

[英]How to replace last <a> tag from section using jquery

I have section as follows 我有以下部分

 <section class="breadCrumb">
     <a href="/">Home</a> <span class="divider">&gt;</span> <a class="CMSBreadCrumbsLink" href="/SCCU.Stg/Events-Calendar.aspx">Events Calendar</a> &gt; 
     <a href="/SCCU.Stg/Events-Calendar/Events-Details.aspx">Events Details</a> 

    </section>

it is dynamically generated by code, in the above section at some places I have tag inside section as follows 它是由代码动态生成的,在上面的部分中的某些地方,我在部分内部具有标签,如下所示

 <section class="breadCrumb">
 <a href="/">Home</a> <span class="divider">&gt;</span> <a class="CMSBreadCrumbsLink" href="/SCCU.Stg/Events-Calendar.aspx">Events Calendar</a> &gt; 
 <span class="CMSBreadCrumbsCurrentItem">Events Details</span>

</section>

So, if you observed above both the HTML code section, you will find one anchor tag and in another section I have span tag, so basically I want to find if Section having class name "breadCrumb" contains last tag attribute I want to convert/replace this anchor tag with span tag and this span class as "CMSBreadCrumbsCurrentItem". 因此,如果您在两个HTML代码部分的上方都观察到了,您将找到一个锚标记,而在另一部分中我具有span标记,因此基本上我想确定类名称为“ breadCrumb”的部分是否包含我要转换的最后一个标记属性/将此锚标签替换为span标签,并将此span类替换为“ CMSBreadCrumbsCurrentItem”。 How can we do that using jquery. 我们如何使用jquery做到这一点。

In above two sections first section has tag at last and another section don't have tag so code would execute only for first section not for second section. 在以上两个部分中,第一个部分最后带有标签,而另一个部分没有标签,因此代码仅对第一部分执行,而对第二部分不执行。

Something similar to this would make it nicely: 与此类似的东西会很不错:

$("#breadCrumb").(":last").
contents().unwrap().wrap('<span/>').addClass("CMSBreadCrumbsCurrentItem");

希望这能解决您的问题
$(".breadCrumb").append("<span class='CMSBreadCrumbsCurrentItem'>" + $(".breadCrumb a").last().html() +"</span>"); $(".breadCrumb a").last().html()

Use following script: 使用以下脚本:

if($(".breadCrumb a").last().length){    
  $(".breadCrumb a").last().remove();
  $(".breadCrumb").append('<spanclass="CMSBreadCrumbsCurrentItem">Events Details</span>');

}
$('.breadCrum').each(function(){
  var l = $('>:last-child', this);
  if (l.prop('nodeName')=='A') {
    l.contents().unwrap().wrap('<span class="CMSBreadCrumbsCurrentItem"></span>');
  }
});

Edit: added > to :last, and changed :last to :last-child and changed tagName to nodeName (sorry using ipad to type my answer) 编辑:>添加到:last,并将:last更改为:last-child,并将tagName更改为nodeName(对不起,使用ipad输入我的答案)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM