简体   繁体   English

jQuery-用所有元素的新值替换属性值

[英]jQuery - replace attribute value with new value of all elements

I've this html markup: 我有这个html标记:

<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'); 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 更多信息在这里

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

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