简体   繁体   中英

Adding line numbers to table <td> using javascript

I am a beginner with javascript. I have an html table with contenteditable data, I would like to use javascript to add numbering next to each line inside the "contenteditable" column after entering TAB:
在此处输入图像描述

 <table> <tr> <td contenteditable="true" >First column</td> </tr> </table>

Any help would be appreciated. Thanks!

If by "line number", you mean "column number", this will work:

 <table> <tr> <td contenteditable="true" >First column</td> <td contenteditable="true" >Second column</td> </tr> </table> <script> let index = 1 document.querySelectorAll('td').forEach((td) => { td.innerText += " " + index index += 1 }) </script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM