【发布时间】:2014-11-05 18:33:06
【问题描述】:
如何使用纯 JavaScript 做到这一点?
我需要清晰的代码或指南。
【问题讨论】:
-
问题解释不够。也没有证据表明 TS 试图以某种方式解决它。没有标记,没有代码尝试——什么都没有。 -1
-
我猜你应该在文本区域的左边添加一个。然后你可以在元素中添加一些数字。
标签: javascript numbers textarea
如何使用纯 JavaScript 做到这一点?
我需要清晰的代码或指南。
【问题讨论】:
标签: javascript numbers textarea
根据我从您的问题中收集到的信息,我认为您需要的是。希望它能让你朝着正确的方向前进。
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>
【讨论】: