根据父列表项的类名动态添加href [英] Dynamically add href based on Class name of parent list item
本文介绍了根据父列表项的类名动态添加href的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要根据其父列表项的类名称将一个 href
属性添加到锚标记。
示例:以下是我需要的:
I need to add a href
attribute to an anchor tag based on the class name of its parent list item.
Example: Here is what I need:
<li class="prefix_1"><a href="prefix_1">myText</a></li>
<li class="prefix_2"><a href="prefix_2">myText</a></li>
<li class="prefix_3"><a href="prefix_3">myText</a></li>
我还应该补充一点,我使用<$ c $动态地将类名添加到列表项中c> jQuery 就像这样:
I should also add that I am dynamically adding the class names to the list items using jQuery
like this:
$("ul.tabs-nav").children().each(function(i) {
$(this).addClass("prefix_" + (i+1));
});
推荐答案
试试这个:
$("ul.tabs-nav").children().each(function(i) {
var $this = $(this); // don't look for the same element twice
$this.addClass("prefix_" + (i+1));
$this.find('a').attr('href', 'prefix_' + (i+1)); // find the link and add href
});
这篇关于根据父列表项的类名动态添加href的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文