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循环生成的图标附加到列表项锚点中
				
        
 
            
        基础教程推荐
- 如何使用sencha Touch2在单页中显示列表和其他标签 2022-01-01
 - WatchKit 支持 html 吗?有没有像 UIWebview 这样的控制器? 2022-01-01
 - 为什么我在 Vue.js 中得到 ERR_CONNECTION_TIMED_OUT? 2022-01-01
 - jQuery File Upload - 如何识别所有文件何时上传 2022-01-01
 - 如何在特定日期之前获取消息? 2022-01-01
 - Node.js 有没有好的索引/搜索引擎? 2022-01-01
 - 什么是不使用 jQuery 的经验技术原因? 2022-01-01
 - 每次设置弹出窗口的焦点 2022-01-01
 - 如何使用 CSS 显示和隐藏 div? 2022-01-01
 - Javascript 在多个元素上单击事件侦听器并获取目标 2022-01-01
 
    	
    	
    	
    	
    	
    	
    	
    	
						
						
						
						
						
				
				
				
				