我有一个有限排的textarea,我有一个按钮,一旦我点击它,它将添加我的名字。 我希望我的名字在第8行,即使我写了第1行到第7行的文本。
请问有办法吗?
这是我的代码,它只会在textarea上的消息后添加我的名字:
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);
答案 0 :(得分:0)
以下是我所说的一个例子:
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