【发布时间】:2014-08-04 23:34:19
【问题描述】:
我有一堆<a> 标签,其中包含不同的字符串。我希望能够根据每个单独标签内的文本设置这些标签的 url。
例如,我有
<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)
});
【问题讨论】:
-
使用循环遍历元素并花时间阅读一些 jQuery 初学者教程。
-
为什么你不能直接生成你的
a标签并正确设置href属性?如果你真的不能,也许知道.attrcan accept a function as second parameter 可以帮助你。
标签: javascript jquery html href