Javascript 将fontawesome图标添加到Select2V4下拉列表项中
我试图在Select2V4下拉列表中显示图标。但是下拉菜单显示的是html,而不是实际的图标。此方法适用于select2 V3,但似乎不适用于v4。感谢您的帮助。多谢各位 HTMLJavascript 将fontawesome图标添加到Select2V4下拉列表项中,javascript,jquery,jquery-select2-4,fontawesome-4.4.0,Javascript,Jquery,Jquery Select2 4,Fontawesome 4.4.0,我试图在Select2V4下拉列表中显示图标。但是下拉菜单显示的是html,而不是实际的图标。此方法适用于select2 V3,但似乎不适用于v4。感谢您的帮助。多谢各位 HTML 运球 升降箱 脸谱网 JS 函数iformat(图标){ var originalOption=icon.element; 返回“”+icon.text; } $('.icons_select2')。选择2({ 宽度:“100%”, 模板选择:iformat, 模板结果:iformat }); 有关示例,请参见
运球
升降箱
脸谱网
JS
函数iformat(图标){
var originalOption=icon.element;
返回“”+icon.text;
}
$('.icons_select2')。选择2({
宽度:“100%”,
模板选择:iformat,
模板结果:iformat
});
有关示例,请参见小提琴:
您可以使用
$.parseHTML()
包装退货
示例:return$.parseHTML(“”+icon.text)代码>这是您的更新
您必须将元素包装在jquery中,如下所示:
function iformat(icon) {
var originalOption = icon.element;
return $('<span><i class="fa ' + $(originalOption).data('icon') + '"></i> ' + icon.text + '</span>');
}
$('.icons_select2').select2({
width: "100%",
templateSelection: iformat,
templateResult: iformat,
allowHtml: true
});
函数iformat(图标){
var originalOption=icon.element;
返回$(''+icon.text+'');
}
$('.icons_select2')。选择2({
宽度:“100%”,
模板选择:iformat,
模板结果:iformat,
allowHtml:是的
});
使用“escapeMarkup”选项,如下所示
$('.icons_select2').select2({
width: "100%",
templateSelection: iformat,
templateResult: iformat,
escapeMarkup: function(m) {
return m;
}
});
FYI如果为templateSelection/templateResult重写函数返回字符串,则将对其进行转义(除非您还重写了escapeMarkup函数),但是如果返回jquery对象,则不会对其进行转义
有些示例还忽略了没有和id的格式输入
if (!icon.id) { return icon.text; }
看到这把小提琴了吗我怎样才能将它添加到select2的输入搜索中?
$('.icons_select2').select2({
width: "100%",
templateSelection: iformat,
templateResult: iformat,
escapeMarkup: function(m) {
return m;
}
});
if (!icon.id) { return icon.text; }