javascript - 获取文本区域中的行号

标签 javascript jquery html textarea

<分区>

Possible Duplicate:
Find out the 'line' (row) number of the cursor in a textarea

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

这可以使用 javascript 和 jquery 吗?

最佳答案

这适用于按钮点击。从 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" />​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​

关于javascript - 获取文本区域中的行号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12553025/

相关文章:

html - 如何通过任何地方或外部点击关闭这个纯 css 弹出窗口?

javascript - 如何用jsunit测试ajax?

javascript - 为什么 react-redux 会创建多个状态?

jquery - 设置 div(s) 的宽度等于更宽的 div

javascript - 如何在显示月份数据时删除特定日期列?

html - 如何在悬停时用另一个替换 div(仅限 CSS)

javascript - 提高 jQuery 函数的效率

javascript - 找到二进制搜索结果的最左重复项

javascript - jquery ajax post请求不接受绝对urlrails应用

html - CSS表格单元格在div上有空间