javascript - 找出文本区域中光标的 'line'(行)号

标签 javascript textarea

我想找出并跟踪文本区域中光标的“行号”(行)。 (“更大的图景”是在每次创建/修改/选择新行时解析该行上的文本,当然,如果文本没有粘贴进去。这可以节省在设定的时间间隔不必要地解析整个文本。)

StackOverflow 上有几篇帖子,但没有一个具体回答我的问题,大多数问题都是关于光标位置(以像素为单位)或在文本区域之外显示行号。

我的尝试如下,从第 1 行开始并且不离开文本区域时效果很好。当单击文本区域并在另一行返回该文本区域时,它会失败。将文本粘贴到其中时也会失败,因为起始行不是 1。

我的 JavaScript 知识非常有限。

<html>

<head>
<title>DEVBug</title>

<script type="text/javascript">

    var total_lines = 1; // total lines
    var current_line = 1; // current line
    var old_line_count;

    // main editor function
    function code(e) {

        // declare some needed vars
        var keypress_code = e.keyCode; // key press
        var editor = document.getElementById('editor'); // the editor textarea
        var source_code = editor.value; // contents of the editor

        // work out how many lines we have used in total    
            var lines = source_code.split("\n");
            var total_lines = lines.length;

    // do stuff on key presses
    if (keypress_code == '13') { // Enter
        current_line += 1;
    } else if (keypress_code == '8') { // Backspace
        if (old_line_count > total_lines) { current_line -= 1; }
    } else if (keypress_code == '38') { // Up
        if (total_lines > 1 && current_line > 1) { current_line -= 1; }
    } else if (keypress_code == '40') { // Down
        if (total_lines > 1 && current_line < total_lines) { current_line += 1; }
    } else {
        //document.getElementById('keycodes').innerHTML += keypress_code;
    }

    // for some reason chrome doesn't enter a newline char on enter
    // you have to press enter and then an additional key for \n to appear
    // making the total_lines counter lag.
    if (total_lines < current_line) { total_lines += 1 };

    // putput the data
    document.getElementById('total_lines').innerHTML = "Total lines: " + total_lines;
    document.getElementById('current_line').innerHTML = "Current line: " + current_line;

    // save the old line count for comparison on next run
    old_line_count = total_lines;

}

</script>

</head>

<body>

<textarea id="editor" rows="30" cols="100" value="" onkeydown="code(event)"></textarea>
<div id="total_lines"></div>
<div id="current_line"></div>

</body>

</html>

最佳答案

您可能需要使用 selectionStart 来执行此操作。

<textarea onkeyup="getLineNumber(this, document.getElementById('lineNo'));" onmouseup="this.onkeyup();"></textarea>
<div id="lineNo"></div>

<script>

    function getLineNumber(textarea, indicator) {

        indicator.innerHTML = textarea.value.substr(0, textarea.selectionStart).split("\n").length;
    }

</script>

当您使用鼠标更改光标位置时,这也有效。

关于javascript - 找出文本区域中光标的 'line'(行)号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9185630/

相关文章:

javascript - 更改/追加文本区域中的值

html - 文本区域 100% 宽度带边距

javascript - 文本区域是必填字段吗?

xhtml - 为什么单个文本区域会弄乱 xhtml 后面的所有内容?

javascript - 指针事件和不透明度在 IE10 中不起作用

javascript - 如何制作模态图库?无需查询

JavaScript : String to a two dimesional Array

javascript - 我如何根据这段 JavaScript 代码访问此属性?

javascript - 导航滚动和点击 anchor 标签

jQuery this() 用于文本区域?