简体   繁体   中英

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". How can we do that using 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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