0

我有一个应用程序,用户输入一个单词并出现一个自动完成。When the select the appropriate autocomplete, it's added to the inputfield and the user keeps typing. 我非常想在输入框中突出显示该单词(而不是在自动完成列表中),并且不突出显示框中的其余单词,但我找不到任何示例。

$(searchBoxId).autocomplete({
    source:      keys,
    delay:       0,
    selectFirst: true,
    select:      function(event, ui) {
        var TABKEY = 9;
        this.value = ui.item.value;

        if (event.keyCode == TABKEY) {
            event.preventDefault();
            this.value = this.value + " ";
            // XXX something goes here?
            this.focus();
        }

        return false;
    },
    autoFocus: true,
    minLength: 2
});

这是 jquery 1.8.20。通过“突出显示”,我的意思是“能够以类似于 Facebook 或 Google+ 突出显示用户名的方式使其在视觉上看起来与众不同”。

4

1 回答 1

1

由于您已经在使用 jQuery,您可能希望使用jQuery Tags 之类的东西。

它支持自动完成并执行您正在寻找的数据差异化。

基本概念是您将文本框contenteditable设置为带有标志的 div。监听keypress事件——特别是空格键和回车键。当它们被击中时,您获取最近输入的文本(尚未“标记”)并创建一个<div>并附加它。

或者只是使用插件...

于 2012-06-19T12:56:13.407 回答