【问题标题】:Add icon in typeahead input box在 typeahead 输入框中添加图标
【发布时间】:2018-12-18 06:21:00
【问题描述】:

HTML 代码 -

<div class="input-group-btn">
   <div id="custom-templates">
       <input class="typeahead caret" id="cur1" name="cur1" onkeypress="getData()" class="form-control" style=" color: black;text-transform: uppercase;border: 0px;border-radius: 0px 5px 5px 0px;" aria-label="You Send" type="text" placeholder="BTC" value="BTC">
   </div>
</div>

Typeahead JS 代码 -

var bestPictures = new Bloodhound({
  datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'),
  queryTokenizer: Bloodhound.tokenizers.whitespace,
  prefetch: 'http://localhost/api/currency.php',
});


$('#custom-templates .typeahead').typeahead(null, {
  name: 'best-pictures',
  display: 'value',
  source: bestPictures,
  templates: {
    empty: [
      '<div class="empty-message">',
        'unable to find any crypto currency',
      '</div>'
    ].join('\n'),
    suggestion: Handlebars.compile('<div><img src="{{image}}" class="svg">&nbsp;&nbsp;&nbsp;<strong style="text-transform: uppercase;">{{value}}</strong> – {{year}}</div>')
  }
});

输出 -

如输出所示,图像已添加到下拉列表中。有没有办法在输入框中也显示图标?就像单击 BTC(见截图)一样,Image/ICON 应该与名称一起添加到输入框中。

【问题讨论】:

标签: javascript twitter-bootstrap typeahead.js typeahead


【解决方案1】:

通过使用:

&lt;span&gt;&lt;i class="fa fa-bitcoin blue"&gt;&amp;nbsp;&lt;/i&gt;输入前和输入后的CSS如下:

.blue{
color:blue;
}

或者:

&lt;input id="icon" style="text-indent:20px;" type="text" placeholder="BTC" /&gt;

CSS 如下:

 background-image:url(https://i.stack.imgur.com/SEjw8.png); /*change with your image*/
 background-repeat: no-repeat; 
 background-position: 2px 2px; 
 background-size: 12px 12px; 

.blue{
color:blue;
}

#icon{
background-image:url(https://i.stack.imgur.com/SEjw8.png); /*change with your image*/
background-repeat: no-repeat; 
background-position: 2px 2px;
background-size: 12px 12px;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet"/>

<div class="input-group-btn">
   <div id="custom-templates">
       <span><i class="fa fa-bitcoin blue">&nbsp;</i><input class="typeahead caret" id="cur1" name="cur1" onkeypress="getData()" class="form-control" style=" color: black;text-transform: uppercase;border: 0px;border-radius: 0px 5px 5px 0px;" aria-label="You Send" type="text" placeholder="BTC" value="BTC"></span><br>
       <input id="icon" class="typeahead caret" id="cur1" name="cur1" onkeypress="getData()" class="form-control" style="text-indent:20px; color: black;text-transform: uppercase;border: 0px;border-radius: 0px 5px 5px 0px;" aria-label="You Send" type="text" placeholder="BTC" value="BTC">
   </div>
</div>

更新:

要转换:FontAwesome Icons to Image

更多参考请查看here(1)here(2) em>

Background-size

Background-position

【讨论】:

  • 要动态更新图标,我建议你在HTML本身中取出background-image: url(...)。因此,只从 css 中删除该行并将其放入 HTML 中,如下所示:&lt;input class="typeahead caret" ... style="background-image: {{image}}" ...&gt;。您必须将所选元素的 image-src 存储在 image 变量中。
猜你喜欢
  • 2012-12-10
  • 2013-05-28
  • 1970-01-01
  • 2017-04-10
  • 2018-05-25
  • 1970-01-01
  • 2014-01-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多