jquery - 如何在 chosen.js 选择控件中添加左侧 Font Awesome 图标?

标签 jquery css html-select font-awesome jquery-chosen

我开始使用 chosen.js 插件来自定义 html select 控件。它工作得很好......但我需要在其中嵌入一个字体很棒的图标,左侧定位,以及大约 30 像素的文本填充(不重要)。

有谁知道我该怎么做?我一无所知。

提前致谢。

最佳答案

假设您的标记是这样的:

<select id="my-select" class="chosen-with-icon">
  <option value="1">Option 1</option>
  <option value="2">Option 2</option>
</select>

以下 CSS 代码将在输入的左侧插入 FontAwesome“用户”图标:

select.chosen-with-icon + .chosen-container.chosen-container-single .chosen-single {
    padding-left    : 35px;
}

select.chosen-with-icon + .chosen-container.chosen-container-single .chosen-single:before {
    content : "\f007";
    display : block;
    font-family : FontAwesome;
    left    : 0;
    position    : absolute;
    text-align  : center;
    width   : 30px;
}

要更改图标,您必须将 content 属性中的 f007 替换为您可以在 FontAwesome 网站上找到的图标的 unicode 代码。 例如,您可以在此处找到星形图标的代码 http://fortawesome.github.io/Font-Awesome/icon/star/

如果你想要花哨并模仿 Bootstrap 输入组样式,你可以这样做:

select.chosen-with-icon + .chosen-container.chosen-container-single .chosen-single {
    padding-left    : 35px;
}

select.chosen-with-icon + .chosen-container.chosen-container-single .chosen-single:before {
    background-color    : #f0f0f0;
    border-right    : 1px solid #ccc;
    content : "\f007";
    display : block;
    font-family : FontAwesome;
    height  : 100%;
    left    : 0;
    line-height : 25px;
    position    : absolute;
    text-align  : center;
    top : 0;
    width   : 30px;
}  

多选的CSS大致相同,只是替换

select.chosen-with-icon + .chosen-container.chosen-container-single .chosen-single

select.chosen-with-icon + .chosen-container-multi .chosen-choices

select.chosen-icon + .chosen-container.chosen-container-single .chosen-single:before

通过

select.chosen-icon + .chosen-container-multi .chosen-choices:before 

关于jquery - 如何在 chosen.js 选择控件中添加左侧 Font Awesome 图标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22131175/

相关文章:

javascript - 如果我从桌面进行开发,我应该使用什么作为文件路径?

jquery - jqgrid - postData 提交之前的请求参数和数据

css - 如何强制 CSS url 尊重 webpack 的 publicPath 设置?

html - 如何使用 css 设置 <option> 标签的宽度和 <select> 标签?

javascript - 如何在javascript中存储变量以限制http调用的数量?

javascript - 如何通过变量 attr val 找到选择器

html - 3个相同宽度的flexbox元素的水平居中

javascript - 每次单击每个链接或 url 时如何更改我网站的背景图片?

html - 样式选择选项

javascript - jQuery 根据选择选项显示/隐藏 div