如何使用纯Javascript将行号添加到textarea?

时间:2014-11-05 18:33:06

标签: javascript numbers textarea

如何使用Pure JavaScript?

我需要明确的代码或指导原则。

1 个答案:

答案 0 :(得分:2)

从我从你的问题中收集到的信息,我相信你需要的东西。希望它会让你朝着正确的方向前进。

        <!DOCTYPE html>
      <html>
        <head>


    <title>text area line number example</title>
     </head>

      <body >


    <textarea id="user-input" name="user-input" rows="15" cols="40">


      Hello is it working?

      I think so.


    </textarea>

       <script>

       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("");


          </script>       

        </body>
         </html>