简体   繁体   English

带下拉菜单的HTML下拉菜单

[英]Html dropdown with fontawesome icons

What i have: An html dropdown like this: 我所拥有的:像这样的html下拉列表:

<select name="faDropdown" class="form-control"> <option value="1">Select an icon</option> </select>

What i want: The dropdown to be filled with fontawesome icons. 我想要的是:下拉菜单中会填满字体很棒的图标。 (not hardcoded like: <option value="fa fa-icon"></option> because I don't want to hardcode like 100 different icons in my dropdown). (而不是像<option value="fa fa-icon"></option>这样的硬编码,因为我不想在下拉菜单中对100个不同的图标进行硬编码)。

I don't know if there is a solution for this so any help would be great :) 我不知道是否有解决方案,所以任何帮助都会很棒的:)

Do you want to use vanilla JS? 您要使用香草JS吗? You can try this example, but how it will work depends on how you want to use it. 您可以尝试此示例,但是它如何工作取决于您要如何使用它。 And anyway you will need to hardcode the icons in the array. 无论如何,您将需要对数组中的图标进行硬编码。

If you have questions describe please where you want to place this code. 如果您有任何疑问,请描述一下您要放置此代码的位置。

 var select = document.getElementsByClassName('form-control')[0]; var icons = ['fa-icon', 'fa-icon2', 'fa-icon3']; for (var i = 0; i < icons.length; i++){ var opt = document.createElement('option'); opt.value = `fa ${icons[i]}`; opt.innerHTML = opt.value; select.appendChild(opt); } 
 <select name="faDropdown" class="form-control"> <option value="1">Select an icon</option> </select> 

I guess you could do that? 我想你能做到吗?

("&#xf0c1;" is just an example of a fontawesome icon).

<select name="faDropdown" class="form-control fa">
<option value="1">&#xf0c1;</option>
</select>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM