jQuery 如果换行,则将 +1 添加到行数
我正在在线制作一个javascript代码编辑器,现在我正在编辑器的左侧进行行数统计。我不知道如何做到这一点,因此当创建新行时,它会将行数添加+1,因此每个新行都会在左侧显示行号。有人知道这样做的好方法吗?
I am making a javascript code editor online, and right now I'm doing the line count on the left side of the editor. I can't figure out how to make it so when a new line is created, it adds +1 to the line count, so each new line it it will have the line number on the left. Anyone know a good way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用
string.split()
来拆分将每个出现的情况放入一个数组中,然后获取数组计数。小提琴
http://jsfiddle.net/WkVb9/
You can use
string.split()
to split each occurrence into an array, then get the array count.Fiddle
http://jsfiddle.net/WkVb9/
计算字符串中 \n 实例的数量,并将其递归地应用于包含数字的左侧 div。
Count the number of \n instances in a string, and apply it recursively to the left side div that contains the numbers.
检查每个输入,如果是
\n
,则 + 1。希望看到更好的答案...Check each input, if it is a
\n
, just + 1. Hope to see a better answer...