我最近使用了JQuery UI自动完成功能,想知道如何在列表顶部添加一个标题。
这是我目前的脚本
$(document).ready(function() {
$('.search').autocomplete({
source: 'search.php',
minLength: 1,
select: function(event,ui) {
var postid = ui.item.id;
if(postid != '') {
location.href = "post/" + postid;
}
}
});
});
我想添加 <span>Suggestions</span>
.
看起来你需要通过覆盖自动完成项目的渲染方式来将其添加为一个 "类别"(见......)。https:/jqueryui.comautocomplete#categories)。). 代码未经测试,但可能会让你走上正确的道路。
$('.search').autocomplete({
source: 'search.php',
minLength: 1,
select: function(event,ui) {
var postid = ui.item.id;
if (postid != '') {
location.href = "post/" + postid;
}
},
_create: function() {
this._super();
this.widget().menu( "option", "items", "> :not(.ui-autocomplete-category)" );
},
_renderMenu: function( ul, items ) {
var self = this;
// Add your unselectable "category"
ul.append( "<li class='ui-autocomplete-category'>SUGGESTIONS</li>" );
// Add your other items
$.each( items, function( index, item ) {
self._renderItem( ul, item );
});
});
});