简体   繁体   English

根据标签中的文字更改href

[英]Change href based on text in a tag

I have a bunch of <a> tags with different strings in them. 我有一堆带有不同字符串的<a>标签。 I want to be able to set the url of those tags based on the text inside each individual tag. 我希望能够根据每个单独标签内的文本设置这些标签的网址。

For example, I have 例如,我有

<a class="list-group-item">example</a>
<a class="list-group-item">example2</a>
<a class="list-group-item">example3</a>

and I want to be able to make the url of the first one equal to /example/ , the second to /example2/ and the third to /example3/ . 并且我希望能够使第一个的URL等于/example/ ,第二个等于/example2/ ,第三个等于/example3/

This jQuery function makes all of the href="/example/" 此jQuery函数使所有href="/example/"

$(function(){
    customURL = $( ".list-group-item" ).html();
    $("a").attr("href", customURL)
});
$('a.list-group-item').attr('href', function(){
    return '/' + $(this).text() + '/';
})

jsFiddle example jsFiddle示例

Use this jQuery: 使用以下jQuery:

$('.list-group-item').each(function(){
   var $this=$(this);
   var customURL=$this.html();
   $this.attr('href',customURL);
});

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

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