我有一个行有限的文本区域,我有一个按钮,只要我点击它,它就会添加我的名字。我希望我的名字在第 8 行,即使我写了从第 1 行到第 7 行的文本。
有没有办法做到这一点?
这是我的代码,它只会在文本区域的消息后添加我的名字:
const messageTextarea = (<HTMLInputElement>document.getElementById('message'));
const selecStart = messageTextarea.selectionStart;
const selectEnd = messageTextarea.selectionEnd;
console.log(selectEnd)
let newmes = '' + this.message;
newmes = newmes.substring(0, selecStart)+ newmes.substring(selectEnd, newmes.length)+this.user.firstname;
this.updateView(newmes);
以下是我所说的一个例子:
let someText = "Hi n How n are n you?"; //This would be replaced with your textarea value
let stringSplit = someText.split('n');
if (stringSplit.length < 8)
{
//Add enough items to be 8 rows
for(let x = stringSplit.length; x < 8; x++)
{
stringSplit.push("");
}
}
//replace the 8th row value with your name
stringSplit[7] = "YourName";
//Join the array back into a string separated by newline between each element value
someText = stringSplit.join('n');
//Set your textarea value = someText