【问题标题】:How to add line numbers to textarea only with pure Javascript? [closed]如何仅使用纯 Javascript 将行号添加到 textarea? [关闭]
【发布时间】:2014-11-05 18:33:06
【问题描述】:

如何使用纯 JavaScript 做到这一点?

我需要清晰的代码或指南。

【问题讨论】:

  • 问题解释不够。也没有证据表明 TS 试图以某种方式解决它。没有标记,没有代码尝试——什么都没有。 -1
  • 我猜你应该在文本区域的左边添加一个
    。然后你可以在
    元素中添加一些数字。

标签: javascript numbers textarea


【解决方案1】:

根据我从您的问题中收集到的信息,我认为您需要的是。希望它能让你朝着正确的方向前进。

var textAreaID = "user-input";

//turn the text area content into an array
var content = document.getElementById(textAreaID).innerHTML.split("\n");

//create array to hold new Content
var newContent = [];

//loop through and add line numbers
for (var i = 0; i < content.length; i++) { //begin for loop


  //append the line numbers and the new value to the newContent array
  newContent.push((i + 1) + content[i] + "\n");


} //end for loop


//update the content of textArea with line numbers
document.getElementById(textAreaID).innerHTML = newContent.join("");
<textarea id="user-input" name="user-input" rows="15" cols="40">


      Hello is it working?

      I think so.


</textarea>

【讨论】:

  • 谢谢你...这是一个好的开始。我要自己开发这个
猜你喜欢
  • 2015-09-22
  • 2017-07-17
  • 2012-05-05
  • 1970-01-01
  • 2020-09-15
  • 2020-01-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多