I am trying to embed an icon in an option from a select list. Using font-awesome icons, no icon is being displayed.
我试图从选择列表中的选项中嵌入一个图标。使用font-awesome图标,不显示任何图标。
<select>
<option><i class="icon-camera-retro"></i> Doesn't work in option!</option>
</select>
Can i use font-awesome to achieve what i need? Or do i have to scrap using font-awesome and do a manual CSS background for each option?
我可以使用font-awesome来实现我所需要的吗?或者我是否必须使用font-awesome废弃并为每个选项执行手动CSS背景?
JSFiddle: https://jsfiddle.net/mmXh2/1/
JSFiddle:http://jsfiddle.net/mmXh2/1/
5 个解决方案
#1
19
You can use FontAwesome as a unicode font and insert your icons in a cross-platform way. You just need to look up the unicode value for each icon
您可以将FontAwesome用作unicode字体,并以跨平台方式插入图标。您只需要查找每个图标的unicode值
<select style="font-family: 'FontAwesome', Helvetica;">
<option> Now I show the pretty camera!</option>
</select>
#2
5
You can cheat a little bit and put the class on the option:
你可以欺骗一点,把课程放在选项上:
https://jsfiddle.net/mmXh2/2/
<option class="icon-camera-retro"> Doesn't work in option!</option>
#3
2
Apparently Select2 (https://ivaynberg.github.io/select2/) is a solution to putting icons in option tags. However, perhaps due to my lack of knowledge, I just couldn't make it work. In the end I resorted to using lists (I was also using Bootstrap)
显然,Select2(https://ivaynberg.github.io/select2/)是将图标放入选项标签的解决方案。然而,也许是由于我缺乏知识,我无法使其发挥作用。最后我使用了列表(我也使用了Bootstrap)
<a class="btn dropdown-toggle category" data-toggle="dropdown" href="/go.html?url=#">All Categories <span class="caret pull-right"></span></a>
<ul id="category" class="dropdown-menu">
<li><a href="/go.html?url=#"><i class="icon"></i> Category A</a></li>
<li><a href="/go.html?url=#"><i class="icon"></i> Category B</a></li>
..
</ul>
There is a drawback though, the id of the list has to be unique. So, if like me you had 5 different lists in one page, you have to declare (?) all of them in javascript making the codes chunky.
但是有一个缺点,列表的id必须是唯一的。所以,如果像我一样,你在一个页面中有5个不同的列表,你必须在javascript中声明(?)所有这些列表使代码变得粗糙。
$(function(){
$("#category li a").click(function(){
$(".category").val($(this).text());
});
});
Hope that help shed some light.
希望有所帮助。
#4
#5
0
A solution for icons working in Chrome has been given on a similar question.
针对在Chrome中工作的图标的解决方案也提供了类似的问题。
JSFiddle示例
Code used:
使用的代码:
function format(icon) {
var originalOption = icon.element;
return '<i class="fa ' + $(originalOption).data('icon') + '"></i> ' + icon.text;
}
$('.wpmse_select2').select2({
width: "100%",
formatResult: format
});