[英]How can I get the value of the first and second line in a textarea?
I'm trying to get the value of the first and second line from a textarea in HTML.我正在尝试从 HTML 中的文本区域获取第一行和第二行的值。
The main problem is that the first and second line can be of different lengths, so I can't just copy X amount of characters.主要问题是第一行和第二行可以有不同的长度,所以我不能只复制 X 个字符。
Most of the solutions I've seen include JQuery, which I'm not familiar with, so I would prefer an answer that doesn't involve JQuery.我见过的大多数解决方案都包括我不熟悉的 JQuery,所以我更喜欢不涉及 JQuery 的答案。 However, if you do have an answer that does use JQuery, I'll also give it a go.
但是,如果您确实有使用 JQuery 的答案,我也会试一试。 Here's my code so far:
到目前为止,这是我的代码:
textarea { resize: none; }
<textarea id="c" placeholder="c" spellcheck="false"></textarea>
Assuming that the first and second line are separated by a newline (by pressing enter), you split it by "\\n" then get the second element.假设第一行和第二行被换行符分隔(按回车键),你用“\\n”分割它,然后得到第二个元素。
function getSecondLine() {
var tarea = document.getElementById("c")
var tareavalue = tarea.value;
var secondLine = tareavalue.split("\n")[1];
console.log(secondLine)
}
function getfirstAndsecondLine(inp) { var splittedInput = document.getElementById(inp).value.split('\\n'); var firstLine = splittedInput[0]; var secondLine = splittedInput[1]; console.log('first Line :' + firstLine); console.log(" "); console.log('second Line :' + secondLine); }
<textarea id="c" placeholder="c" spellcheck="false"></textarea> <button onclick=getfirstAndsecondLine('c')> get first and second line </button>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.