jquery自动完成突出显示

Jai*_*oss 21 javascript jquery autocomplete

如何使我的jquery自动完成突出显示用户在任何自动完成的结果中输入的内容?我使用的代码是:

    $("#keyword").autocomplete({ 
        source: "ajax/autocomplete.php?action=keyword", 
        minLength: 2 
    });
Run Code Online (Sandbox Code Playgroud)

尝试通过链接tomasz发布实现:

    $("#keyword").autocomplete({ 
        source: "ajax/autocomplete.php?action=keyword", 
        highlight: function(value, term) { 
    return value.replace(new RegExp("("+term+")", "gi"),'<b>$1</b>'); 
    },
        minLength: 2 
    });
Run Code Online (Sandbox Code Playgroud)

也没有运气.jQuery autocomplete似乎讨厌我.

更新:感谢David Murdoch,我现在有了答案!请参阅@ Herman下面答案的副本.

Her*_*aaf 32

感谢David Murdoch在http://www.vervestudios.co/提供这个有用的答案:

$.ui.autocomplete.prototype._renderItem = function( ul, item){
  var term = this.term.split(' ').join('|');
  var re = new RegExp("(" + term + ")", "gi") ;
  var t = item.label.replace(re,"<b>$1</b>");
  return $( "<li></li>" )
     .data( "item.autocomplete", item )
     .append( "<a>" + t + "</a>" )
     .appendTo( ul );
};
$("#keyword").autocomplete({ 
  source: "ajax/autocomplete.php?action=keyword", 
  minLength: 2 
});
Run Code Online (Sandbox Code Playgroud)