簡體   English   中英

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

[英]How can I get the value of the first and second line in a textarea?

我正在嘗試從 HTML 中的文本區域獲取第一行和第二行的值。

主要問題是第一行和第二行可以有不同的長度,所以我不能只復制 X 個字符。

我見過的大多數解決方案都包括我不熟悉的 JQuery,所以我更喜歡不涉及 JQuery 的答案。 但是,如果您確實有使用 JQuery 的答案,我也會試一試。 到目前為止,這是我的代碼:

 textarea { resize: none; }
 <textarea id="c" placeholder="c" spellcheck="false"></textarea>

假設第一行和第二行被換行符分隔(按回車鍵),你用“\\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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM