jquery - append Icons generated by for loop into list items anchor(jquery - 将for循环生成的图标附加到列表项锚点中)
问题描述
我将非常感谢一些指导甚至解决方案!
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循环生成的图标附加到列表项锚点中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:jquery - 将for循环生成的图标附加到列表项锚点中


- 失败的 Canvas 360 jquery 插件 2022-01-01
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- Flexslider 箭头未正确显示 2022-01-01
- Fetch API 如何获取响应体? 2022-01-01
- addEventListener 在 IE 11 中不起作用 2022-01-01
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
- 400或500级别的HTTP响应 2022-01-01
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01