简体   繁体   中英

jQuery - replace attribute value with new value of all elements

I've this html markup:

<div class="item" path="asaa/basba/asbsaa"></div>
<div class="item" path="bsbab/gbfssv/vsaava"></div>
<div class="item" path="asbaba/bsaba/dfsavava"></div>

The important point is that each is different. I need to get this attributes with something like that that:

var title = $(this).attr('path');

Now I need to separate the attribute: I need only the content which is between the * and the *:

<div class="item" path="asaa/basba/*asbsaa*"></div>
<div class="item" path="bsbab/gbfssv/*vsaava*"></div>
<div class="item" path="asbaba/bsaba/*dfsavava*"></div>

Any ideas to do something like that? Thanks for answers!

Get the attribute of each element and then setting back the last part of it

  $(".item").attr("path",function(){ return $(this).attr("path").split("/").pop(); }) console.log($('body').html()); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="item" path="asaa/basba/asbsaa"></div> <div class="item" path="bsbab/gbfssv/vsaava"></div> <div class="item" path="asbaba/bsaba/dfsavava"></div> 

This is a typicall scenario where I would use the "data" tag

<div class="item" data-path="asaa/basba/asbsaa"></div>

script:

 $(".item").data("path").split("/").pop(); 

more info here

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