我试图创建一个2个文本区域的工具,在该工具中,我向1个文本区域输入多达300行文本,它将删除除前5个字符以外的所有字符,并仅在第二个文本区域中吐出这5个字符
请原谅我的失败尝试,我知道我的代码无处不在,我尝试了多种方法来做到这一点,我相信forloop会是最好的,但是我无法让.substring工作
function lineParse() {
var parseBoxList = document.getElementById("parsebox").value;
var nodeLength = 5;
var trimFat = parseBoxList.substring(0, nodeLength);
var text = document.getElementById("parsebox").value;
var lines = text.split('/n');
var cut = lines.substring(0, 5);
var count = lines.length;
var x = document.getElementById("parsebox").value;
var text2 = "";
var i;
var parseLength = 5;
for (i = 0; i < lines.length; i++) {
var text3 = text2;
text2 += lines[i];
}
document.getElementById("demo").innerHTML = cut;
什么都没发生
答案 0 :(得分:0)
您应该可以使用以下代码实现所需的内容:
HTML
<textarea id="textarea1">
this1 is a line
this2 is a line
this3 is a line
this4 is a line
this5 is a line
</textarea>
<textarea id="textarea2"></textarea>
<button id="execute">Execute</button>
JS
function test() {
// split text area value by newline
var lines = document.getElementById("textarea1").value.split("\n");
var newText = "";
// loop over text area line array and substring each to get the first 5 characters
for (var i = 0; i < lines.length; i++) {
var line = lines[i];
newText += line.substring(0, 5) + "\n";
}
// set values to newText variable
document.getElementById("textarea2").value = newText;
document.getElementById("textarea1").value = newText;
}
document.getElementById("execute").addEventListener('click', test);