Html adding line numbers to textarea

a 夏天 提交于 2019-11-27 00:59:06
anatoly techtonik

There is Lined TextArea mirror plugin for jQuery by Alan Williamson
MIT License
jQuery 1.3+

ChandrasekarG

You can very well try Code Mirror, which is a JavaScript library for embedding a code editor in a web page.

With code lines, it has great features like

  • Autocompletion
  • Themes
  • Mixed language modes
  • Search
  • Merge/diff interface
  • Custom scrollbars etc.

This is a very simple, but effective trick. It inserts an image with the line numbers already added.

The only catch is you may need to create your own image to match your UI design.

https://jsfiddle.net/vaakash/5TF5h/

textarea {
    background: url(http://i.imgur.com/2cOaJ.png);
    background-attachment: local;
    background-repeat: no-repeat;
    padding-left: 35px;
    padding-top: 10px;
    border-color:#ccc;
}

Credit goes to: Aakash Chakravarthy

CodePress is the one used in WordPress.

No one tried to do this using HTML5 Canvas object and by painting line numbers on it. So here what I've managed to pool in few hours. Put canvas and textarea, one next to the other, and painted numbers on canvas.

https://www.w3schools.com/code/tryit.asp?filename=G68VMFWS12UH

true there is limitation we can't handle word-wrap easy in Paint() function without iterating entire textarea content and offdrawing to mirror object for measurements of each line height. Also would yield very complex code.

preview image

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!