如何在textarea
或满足div
我开始用最后写的字符或按键的缓冲区来做,但后来我注意到有多个角落情况:
让我们说用户写 H E L L û BACKSPACE 0
用户使用箭头键移动
用户可以用 SPACE (我正在使用的那些)分隔单词,还可以 ENTER 或逗号等
有没有一种很好的方法来检测最后写的字?
目标:对自动填充功能进行编码,例如:thx + <TAB>
=&gt;文字被&#34取代;非常感谢。&#34;
答案 0 :(得分:1)
您可以使用当前光标位置来检测最后一个单词。下面给出一个简单的例子。
document.getElementById('foobar').addEventListener('keydown', e => {
if( e.which == 9 ) {
e.preventDefault();
let endingIndex = e.target.selectionStart;
let startingIndex = endingIndex && endingIndex - 1;
let value = e.target.value;
// putt all delemeters in it by which word can be splitted
let regex = /[ ]/;
while(startingIndex > -1){
if(regex.test(value[startingIndex])){
++startingIndex;
break;
}
--startingIndex;
}
// note you will have you apply check to avoid negative index
if(startingIndex < 0) {
startingIndex = 0;
}
console.log(value.substring(startingIndex, endingIndex));
let newText = "replaced";
value = value.substring(0, startingIndex) + newText + value.substring(endingIndex);
let cursorPosition = startingIndex + newText.length;
e.target.value = value;
e.target.setSelectionRange(cursorPosition, cursorPosition);
}
});
<textarea id="foobar"></textarea>
答案 1 :(得分:0)
这完全适用于textarea
和contenteditable
div:
var newText = 'hello\nnewline';
var newHtml = '<b>test</b> and<br>a <a href="hjkh">link</a>';
document.addEventListener("keydown", function(e) {
var elt = e.target;
if (elt.tagName.toLowerCase() === 'textarea' || elt.isContentEditable)
{
if (e.keyCode == 9) {
e.preventDefault();
if (elt.isContentEditable) { // for contenteditable
elt.focus();
sel = document.getSelection();
sel.modify("extend", "backward", "word");
range = sel.getRangeAt(0);
console.log(range.toString().trim());
range.deleteContents();
var el = document.createElement("div");
el.innerHTML = newHtml;
var frag = document.createDocumentFragment(), node;
while (node = el.firstChild) {
frag.appendChild(node);
}
range.insertNode(frag);
range.collapse();
} else { // for texterea/input element
var endingIndex = elt.selectionStart;
var startingIndex = endingIndex && endingIndex - 1;
var value = elt.value;
var regex = /[ ]/;
while (startingIndex > -1) {
if (regex.test(value[startingIndex])) {
++startingIndex;
break;
}
--startingIndex;
}
if (startingIndex < 0) {
startingIndex = 0;
}
value = value.substring(0, startingIndex) + newText + value.substring(endingIndex);
var cursorPosition = startingIndex + newText.length;
e.target.value = value;
e.target.setSelectionRange(cursorPosition, cursorPosition);
}
}
}
});
&#13;
<div contenteditable>Hello, press TAB to replace this WORD<br>Also press TAB after this ONE</div>
<textarea rows="8" cols="50" id="a">Hello, press TAB to replace this WORD
Also press TAB after this ONE</textarea>
&#13;