1

我找不到解决方案。我试图假设 \n 符号的计数与行数相同,但有时这种方法工作不正确(例如,从剪贴板粘贴文本后)我尝试了不同的 jQuery 插件,但仍然不成功。任何的想法?

4

3 回答 3

2

为什么不这样做:

仅获取文本内容,selectionStart然后通过拆分使其成为数组eol

p = $('#Form_config').val().substr(0, $('#Form_config')[0].selectionStart).split("\n");

// line is the number of lines
line = p.length;

// col is the length of the last line
col = p[p.length-1].length;
于 2014-01-24T21:29:39.827 回答
1

决定不是那么简单,需要大量的javascript代码。所以,最后我使用了 Marijn Haverbeke 的 CodeMirror 项目(https://github.com/marijnh/CodeMirror)

于 2011-04-13T06:19:04.267 回答
0

尝试使用这个:

var pos = getCaretPos(document.formName.textareaName);

function getCaretPos(obj)
{
  obj.focus();

  if(obj.selectionStart) return obj.selectionStart;//Gecko
  else if (document.selection)//IE
  {
    var sel = document.selection.createRange();
    var clone = sel.duplicate();
    sel.collapse(true);
    clone.moveToElementText(obj);
    clone.setEndPoint('EndToEnd', sel);
    return clone.text.length;
  }

  return 0;
}
于 2011-02-28T10:32:06.163 回答