简体   繁体   English

混合Typeahead.js和Bootstrap 3

[英]Mixing Typeahead.js and Bootstrap 3

I have an app that uses Bootstrap 3 and Typeahead. 我有一个使用Bootstrap 3和Typeahead的应用程序。 For some reason, as soon as I add Typeahead.js , the styling gets misformatted, you can see with this JSFiddle . 由于某种原因,一旦我添加Typeahead.js ,样式就会格式错误,您可以使用此JSFiddle看到 The following HTML looks great: 以下HTML看起来很棒:

<div class="container" style="padding:3rem;">
<form class="form-horizontal">
  <div class="form-group">
    <div class="input-group">
      <input type="search" class="form-control" id="myQuery">
      <div class="input-group-btn">
        <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"><span id="searchIndexButton">Item 1</span> <span class="caret"></span></button>
        <ul class="dropdown-menu dropdown-menu-right">
          <li>Item 1</li>
          <li>Item 2</li>
        </ul>
      </div>
    </div>                            
  </div>
</form>                    
</div>

As soon as I initialize it with Typeahead as shown below, it looks terrible. 如下所示,一旦我用Typeahead初始化它,它看起来就很糟糕。

$(function() {
  var data = [
    { id:1, name:'Tiger' },
    { id:2, name:'Bear' }
  ];

  var bh = new Bloodhound({
    datumTokenizer: Bloodhound.tokenizers.whitespace,
    queryTokenizer: Bloodhound.tokenizers.whitespace,
    local: data
  });

  $('#myQuery').typeahead(null, {
    minLength: 1,
    name: 'suggestions',
    source: bh,
    display: 'name'
  });
});

If you include this CSS code for using typeahead.js with Bootstrap 3 , the HTML will look as expected without modifications: 如果您在Bootstrap 3中包含了用于使用typeahead.js的CSS代码 ,则HTML将按预期显示,而无需进行修改:

JSFiddle: https://jsfiddle.net/2j94perk/ JSFiddle: https ://jsfiddle.net/2j94perk/

Twitter's typeahead don't work direct with Bootstrap 3. The DOM structure of the dropdown menu used by typeahead.js differs from the DOM structure of the Bootstrap dropdown menu. Twitter的typeahead不能直接与Bootstrap 3一起使用。typeahead.js使用的下拉菜单的DOM结构与Bootstrap下拉菜单的DOM结构不同。 You'll need to load some additional CSS in order to get the typeahead.js dropdown menu to fit the default Bootstrap theme. 您需要加载一些其他CSS才能获得typeahead.js下拉菜单以适合默认的Bootstrap主题。 You can download the basic CSS here, or use the LESS file to integrate it into your project. 您可以在此处下载基本CSS,或使用LESS文件将其集成到您的项目中。

Include the CSS file after Bootstrap's CSS in your HTML: 在HTML中的Bootstrap CSS之后添加CSS文件:

<link href="bootstrap-3.1.0/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="typeaheadjs.css" rel="stylesheet">

Source: https://github.com/bassjobsen/typeahead.js-bootstrap-css 来源: https : //github.com/bassjobsen/typeahead.js-bootstrap-css

If you inspect the DOM with typeahead initialized you should notice that the script wraps the <input> with a <span> . 如果检查初始化为typeahead的DOM,则应注意脚本用<span>包装<input> <span> Bootstrap is expecting a certain structure which is no longer the same after typeahead runs. Bootstrap期望在预先输入运行后不再相同的某种结构。

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

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