我在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 + ']]';
}
}
]);
});
插入文本后,它会在句子的最后一个位置显示光标位置。如何在最近添加的文本后设置它?
已更新
我在找到空格并输入单词后将文本保存在数组中,按下退格键后我使用指针并根据它来获取数据..