jQuery UI Autocomplete控件下拉列表固定高度

来源:互联网 发布:windows安装jdk 编辑:程序博客网 时间:2024/05/22 13:32

jQuery ui Autocomplete控件下拉列表高度默认是自适应的,有多少数据就会加载出来多少行。以下是给下拉列表设置一个默认高度并添加滚动条的解决办法。

  • 在样式里添加以下代码
.ui-autocomplete {    max-height: 150px; //默认最大高度150像素    overflow-y: auto; //添加滚动条    /* prevent horizontal scrollbar */    overflow-x: hidden;  }  /* IE 6 doesn't support max-height   * we use height instead, but this forces the menu to always be this tall   */  * html .ui-autocomplete {    height: 150px; //解决IE6下不识别max-height  }
  • 官方源码[http://jqueryui.com/autocomplete/#maxheight ]
<!doctype html><html lang="en"><head>  <meta charset="utf-8">  <meta name="viewport" content="width=device-width, initial-scale=1">  <title>jQuery UI Autocomplete - Scrollable results</title>  <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">  <link rel="stylesheet" href="/resources/demos/style.css">  <style>  .ui-autocomplete {    max-height: 150px; //默认最大高度150像素    overflow-y: auto; //添加滚动条    /* prevent horizontal scrollbar */    overflow-x: hidden;  }  /* IE 6 doesn't support max-height   * we use height instead, but this forces the menu to always be this tall   */  * html .ui-autocomplete {    height: 150px; //解决IE6下不识别max-height  }  </style>  <script src="https://code.jquery.com/jquery-1.12.4.js"></script>  <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>  <script>  $( function() {    var availableTags = [      "ActionScript",      "AppleScript",      "Asp",      "BASIC",      "C",      "C++",      "Clojure",      "COBOL",      "ColdFusion",      "Erlang",      "Fortran",      "Groovy",      "Haskell",      "Java",      "JavaScript",      "Lisp",      "Perl",      "PHP",      "Python",      "Ruby",      "Scala",      "Scheme"    ];    $( "#tags" ).autocomplete({      source: availableTags    });  } );  </script></head><body>  <div class="ui-widget">    <label for="tags">Tags: </label>    <input id="tags">  </div></body></html>
1 0
原创粉丝点击