Javascript 撤消和重做后设置光标焦点

Javascript 撤消和重做后设置光标焦点,javascript,jquery,html,Javascript,Jquery,Html,我在textarea中使用插件进行自动提示,但在chrome中,它在撤销和重做后没有给出适当的答案 我已经完成了,下面是代码: <textarea id="textbox" cols="60" rows="10"> window.onload = function () { var oTextbox = new AutoSuggestControl("textbox"); if (navigator.userAgent.indexOf("Chrom

我在textarea中使用插件进行自动提示,但在chrome中,它在撤销和重做后没有给出适当的答案

我已经完成了,下面是代码:

<textarea id="textbox"   cols="60" rows="10">
window.onload = function () {
        var oTextbox = new AutoSuggestControl("textbox");
        if (navigator.userAgent.indexOf("Chrome") != -1) {
            var arr = new Array();
            var arrLength = 0;
            var string;
            var text = document.getElementById('textbox');
            $("#textbox").on('keyup', function (e) {
                if (arr.length == 0) {
                    arr.push(text.value);
                }
            });
            $("#textbox").on('keydown', function (e) {
                if ((e.which !== 90 && !e.ctrlKey) || (e.which !== 89 && !e.ctrlKey)) {
                    var endPos = doGetCaretPosition(text);
                    if (e.which === 8) {
                        string = text.value.substring(0, endPos - 1);
                        for (var i = 0; i < arr.length; i++) {
                            if (arr[i].includes(string)) {
                                arrLength = i;
                                break;
                            }
                        }
                    }
                    else {
                        var lastText = text.value.substring(endPos - 1, endPos);
                        if (lastText == " ") {
                            console.log
                              (
                               "text.value lasttext " + text.value 
                                + " arrLength " + arrLength
                              );
                            arr.push(text.value);
                            arrLength = arr.length - 1;
                        }
                    }
                }
            });
            //SHORTCUTS OF SECTIONS
            $.ctrlkeycode = function (key, callback, args) {
                $(document).bind('keydown', function (e) {
                    if (!args) args = []; // IE barks when args is null
                    if (e.ctrlKey && e.keyCode == key.charCodeAt(0)) {
                        e.preventDefault();
                        callback.apply(this, args);
                        return false;
                    }
                });
            }

            $.ctrlkeycode('Z', function () {
                console.log("arrLength in z before " + arrLength);
                if (arrLength != 0) {
                    for (var i = 0; i < arr.length; i++) {
                        console.log(arr[i]);
                    }
                    console.log(arrLength + " in undo part");
                    text.value = "";
                    text.value = arr[arrLength];
                    arrLength = arrLength - 1;
                    console.log("arrLength in z after " + arrLength);
                }
            });
            $.ctrlkeycode('Y', function () {
                for (var i = 0; i < arr.length; i++) {
                    console.log(arr[i]);
                }
                console.log
                 (
                   "y called (arr.length > arrLength + 1): " +
                    (arr.length + " > " + arrLength)
                 );
                if (arr.length > arrLength + 1) {
                    arrLength = arrLength + 1;
                    text.value = "";
                    text.value = arr[arrLength];
                    console.log(arrLength + " redo");
                }
            });
        }
    }
       $(document).ready(function(){
               $('#textbox').textcomplete([
    {
        words: ['Ceiling', 'Light fittings', 'Windows and cills', 'Door and frame'],
        match: /\b(\w{2,})$/,
        search: function (term, callback) {
            callback($.map(this.words, function (word) {
                return word.indexOf(term) === 0 ? word : null;
            }));
        },
        index: 1,
        replace: function (word) {
            return '[[' + word + ']]';
        }
    }
]);  
});

window.onload=函数(){
var oTextbox=新的自动建议控件(“文本框”);
if(navigator.userAgent.indexOf(“Chrome”)!=-1){
var arr=新数组();
var-arrLength=0;
var字符串;
var text=document.getElementById('textbox');
$(“#文本框”)。在('keyup',函数(e)上{
如果(arr.length==0){
arr.push(文本值);
}
});
$(“#文本框”).on('keydown',函数(e){
if((e.which!==90&&!e.ctrlKey)| |(e.which!==89&&!e.ctrlKey)){
var endPos=doGetCaretPosition(文本);
如果(e.which==8){
string=text.value.substring(0,endPos-1);
对于(变量i=0;iarrLength+1):”+
(arr.length+“>”+arr.length)
);
如果(arr.length>arr.length+1){
arrLength=arrLength+1;
text.value=“”;
text.value=arr[arrLength];
console.log(arrLength+“redo”);
}
});
}
}
$(文档).ready(函数(){
$('#textbox')。textcomplete([
{
文字:[“天花板”、“灯具”、“窗户和窗框”、“门和门框”],
匹配:/\b(\w{2,})$/,,
搜索:函数(术语、回调){
回调($.map)(this.words,函数(word){
返回word.indexOf(term)==0?word:null;
}));
},
索引:1,
替换:函数(word){
返回'['+word+']]';
}
}
]);  
});
插入文本后,它会在句子的最后显示光标位置。如何在最近添加的文本后设置光标位置

已更新

在找到空格并键入word后,我将文本保存在数组中,在按backspace键后,我使用了指针,并根据该指针获取数据