在文本区域中获取行号 [英] Getting Line Number In Text Area

查看:85
本文介绍了在文本区域中获取行号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
找出行"(行)文本区域中的光标编号

我想在HTML textarea 元素中找到用户光标所在的行号.

I want to find out the line number that a user's cursor in on in a HTML textarea element.

使用javascript和jquery是否可能?

Is this possible using javascript and jquery?

推荐答案

通过单击按钮即可使用.从中被盗文本区域中光标的(行)数

This works with button click. Stolen from Find out the 'line' (row) number of the cursor in a textarea

​function getline()
{
    var t = $("#t")[0];
    alert(t.value.substr(0, t.selectionStart).split("\n").length);
}​

HTML

​<textarea rows="6" id="t">
there is no
love in the ghetto
so come here
and get it
</textarea>
<input type="button" onclick="getline()" value="get line" />​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​

这篇关于在文本区域中获取行号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆