jQuery 如果换行,则将 +1 添加到行数

发布于 2024-09-24 15:03:58 字数 110 浏览 1 评论 0原文

我正在在线制作一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

臻嫒无言 2024-10-01 15:03:58

您可以使用 string.split() 来拆分将每个出现的情况放入一个数组中,然后获取数组计数。

$('textarea').keyup(function() {
    if ($(this).val().lastIndexOf('\n')!=-1)
        x = $(this).val().split('\n');
    $('div').text(x.length); // This will be the line number
});​

小提琴

http://jsfiddle.net/WkVb9/

You can use string.split() to split each occurrence into an array, then get the array count.

$('textarea').keyup(function() {
    if ($(this).val().lastIndexOf('\n')!=-1)
        x = $(this).val().split('\n');
    $('div').text(x.length); // This will be the line number
});​

Fiddle

http://jsfiddle.net/WkVb9/

勿挽旧人 2024-10-01 15:03:58

计算字符串中 \n 实例的数量,并将其递归地应用于包含数字的左侧 div。

Count the number of \n instances in a string, and apply it recursively to the left side div that contains the numbers.

远山浅 2024-10-01 15:03:58

检查每个输入,如果是 \n,则 + 1。希望看到更好的答案...

Check each input, if it is a \n, just + 1. Hope to see a better answer...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文