您应该能够使用正则表达式如下实现你正在寻找的结果。
item.label.replace(new RegExp('(' + term + ')', 'ig'), function ($1, match) {
return '<strong>' + match + '</strong>';
});
完整的例子如下。
$("#project").autocomplete({
minLength: 0,
source: projects,
focus: function(event, ui) {
$("#project").val(ui.item.label);
return false;
},
select: function(event, ui) {
$("#project").val(ui.item.label);
$("#project-id").val(ui.item.value);
$("#project-description").html(ui.item.desc);
$("#project-icon").attr("src", "images/" + ui.item.icon);
return false;
}
})
.data("autocomplete")._renderItem = function(ul, item) {
var term = this.term,
formattedLabel = item.label.replace(new RegExp('(' + term + ')', 'ig'), function ($1, match) {
return '<strong>' + match + '</strong>';
});
return $("<li></li>")
.data("item.autocomplete", item)
.append("<a>" + formattedLabel + "<br>" + item.desc + "</a>")
.appendTo(ul);
};
谢谢。这看起来像我正在寻找的。如果解决问题,我会通知你。 – rickypai
得到它的工作。谢谢。 – rickypai