简体   繁体   English

如何获取textarea中第一行和第二行的值?

[英]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.

相关问题 如何使用js清空第二个textarea中的第一个textarea值 - how to empty the first textarea value in second textarea using js 如何获得第二行(第三行等)的缩进,以使用CSS使用自定义编号匹配列表中的第一行? - How can I get second (third, etc.) line indentation to match first line in a list with custom numbering using CSS? 我怎样才能像这种格式一样将此文本区域值(内容)转换为 DIV - How can i get This textarea value (content) into DIV like this format 我如何从textarea获取价值并垂直打印? - How can i get value from textarea and print it vertically? 如何使用JS或jQuery确定文本区域中的光标在第一行还是最后一行? - How can I work out whether the cursor in a textarea is in the first or last line, using JS or jQuery? 如何在不使用标签或鼠标的情况下将光标从第一个文本区域移至第二个文本区域? - How do I move the cursor from the first textarea to a second textarea, without using tab or the mouse? 获取textarea的第一行值 - Get the first row value of textarea 如何使用Jquery删除textarea的第一行? - How do I remove the first line of a textarea with Jquery? 我如何从 textarea 获取价值并检查它? - How I get value from textarea and check it? 如何获得文本区域的价值? - How do I get the value of a textarea?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM