簡體   English   中英

根據標簽中的文字更改href

[英]Change href based on text in a tag

我有一堆帶有不同字符串的<a>標簽。 我希望能夠根據每個單獨標簽內的文本設置這些標簽的網址。

例如,我有

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

並且我希望能夠使第一個的URL等於/example/ ,第二個等於/example2/ ,第三個等於/example3/

此jQuery函數使所有href="/example/"

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

jsFiddle示例

使用以下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