jQuery-将由for循环生成的图标附加到列表项锚点中 [英] jquery - append Icons generated by for loop into list items anchor
问题描述
我将非常感谢您提供一些指导甚至解决方案!
I would be very grateful for some guidance or even a solution!
从一组类名中,我正在使用for循环生成图标集:
From an array of class names, I'm generating icon sets using a for loop:
// icon array
var iconsArray = ['fa-search','fa-toggle-up','fa-link'];
var iconsArrayLength = iconsArray.length;
// loop through array
for (var i = 0; i < iconsArrayLength; i++) {
// result
console.log('<i class="fa' + ' ' + iconsArray[i] + '"></i>');
}
我也有一个导航元素.列表中具有ID的项目数相同:
I also have a navigation element. The list has the same number of items with an ID:
<nav>
<ul id="nav">
<li><a href="">link 1</a></li>
<li><a href="">link 2</a></li>
<li><a href="">link 3</a></li>
</ul>
</nav>
问如何将返回的图标集附加/添加到正确的导航项上?
Q. How do I append / prepend the returned icon sets to the correct nav item?
要清楚,第一个数组值用于第一个导航项,依此类推.
To be clear, the first array value is intended for the first navigation item and so on.
我敢肯定,有什么东西可以回答这个问题,但是强大的G似乎并不想将其服务于我!
I'm sure there's something out there to answer this question, but the mighty G just doesn't seem to want to serve it up to me!
谢谢.
回应评论...
结果列表如下:
<nav>
<ul id="nav">
<li><a href=""><i class="fa fa-search"></i>link 1</a></li>
<li><a href=""><i class="fa fa-toggle-up"></i>link 2</a></li>
<li><a href=""><i class="fa fa-link"></i>link 3</a></li>
</ul>
</nav>
推荐答案
您甚至不需要for循环
You don't even need the for loop
$('#nav li').each(function(i) {
$(this).append('<i class="fa' + ' ' + iconsArray[i] + '"></i>')
})
这篇关于jQuery-将由for循环生成的图标附加到列表项锚点中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!