简体   繁体   English

如何获得一个可疑的html元素的光标行号?

[英]How can i get the cursor line number of a contenteditable html element?

I need to get the line number with the cursor in it or better the offset of the cursor in a contenteditable body element. 我需要使用光标在其中获取行号,或者更好地获取光标在可满足的body元素中的偏移量。

How can i achieve this? 我怎样才能实现这一目标?

I found a solution to my problem, but it will only work if the line-height of all lines are the same. 我找到了解决问题的方法,但只有当所有行的行高相同时才会有效。 The idea is to insert a dummy element at the caret posititon, calculate the position relative to the body start and then divide this value by the line-height. 我们的想法是在插入符号位置插入一个虚拟元素,计算相对于正文开始的位置,然后将该值除以行高。 The result is the number of line the caret is in. 结果是插入符号所在的行数。

Here some code to get started with: 这里有一些代码可以开始:

        // get lineheight, eighter line-height or min-height
        var $elem = $(ed.getBody().firstElementChild);
        var lineHeight = parseInt($elem.css('line-height'), 10) || parseInt($elem.css('min-height'), 10);

        var rng = ed.selection.getRng();
        rng.collapse(true);

        var bm = ed.selection.getBookmark();
        var $marker = $(ed.getBody()).find('#'+bm.id);
        var elem = ed.getDoc().getElementById(bm.id+'_start');
        try {
            box = elem.getBoundingClientRect();
        } 
        catch(e){}

        var doc = ed.getDoc(),
            docElem = doc.documentElement,
            body = ed.getBody(),
            win = ed.getWin(),
            clientTop  = docElem.clientTop  || body.clientTop  || 0,
            clientLeft = docElem.clientLeft || body.clientLeft || 0,
            scrollTop  = win.pageYOffset || jQuery.support.boxModel && docElem.scrollTop  || body.scrollTop,
            scrollLeft = win.pageXOffset || jQuery.support.boxModel && docElem.scrollLeft || body.scrollLeft,
            top  = box.top  + scrollTop  - clientTop,
            left = box.left + scrollLeft - clientLeft;

        // set Bookmark
        ed.selection.moveToBookmark(bm);

        var caret_line = Math.floor( (top) / lineHeight ) + 1;

Here is one of my older tools on the subject. 这是我关于这个主题的旧工具之一。

/**
 * @description This operation splits the content of a text area based on its cursor position
 *              and selection.
 * @param targetNode This argument expects the text node to be split.
 * @return This operation returns an object containing {first, middle, last}.
 */
var splitTextAreaBySelection = function(targetNode)
{
  var target = document.getElementById('Area_'+targetNode.uid);
  var response = {};

  // IE fails this DOM3 operation. Use IE proprietary code here to recover.
  // WARNING: This proprietary IE code has Win32 platform dependency!
  if (typeof(target.selectionStart) === "undefined")
  {
    var content = targetNode.content;
    // this captures the area selected text.
    var r = document.selection.createRange();
    if (r !== null) {
      // thankfully the middle is easy to get!
      response.middle = r.text;
      // now we must create a new text range and mess with it until the cursor             position can be discovered.
      var re = target.createTextRange();
      var rc = re.duplicate();
      re.moveToBookmark(r.getBookmark());
      rc.setEndPoint('EndToStart', re);          
      var cursorPos = rc.text.length;
      // now that we have the cursor position, we can abstract the first and last         strings from the content.
      response.first = content.substr(0,cursorPos);
      response.last = content.substr(cursorPos + response.middle.length);
    }
    else
    {
      response.first = content;
    }
  }
  else
  {
    response.first = target.value.substring(0,target.selectionStart);
    response.middle = arget.value.substring(target.selectionStart, target.selectionEnd);
    response.last = target.value.substring(target.selectionEnd, target.textLength);
  }
  return response;
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何获取包含HTML的contentEditable中的光标位置? - How to get the cursor position in contentEditable containing HTML? 如何以编程方式模拟输入内容可编辑的 HTML 元素? - How can I programmatically simulate typing in a contenteditable HTML element? 如何在 contenteditable 环境中获得被点击的行? - How can I get the line that is being clicked on in a contenteditable environment? 使用contentEditable时,如何使用Javascript获取插入符号元素? - How can I get the element the caret is in with Javascript, when using contentEditable? 如何在contentEditable-DIV中获取HTML代码 - How can I get HTML-Code in a contentEditable-DIV 如何使元素靠近光标? - How can I get the element near the cursor? 在contenteditable元素中,在HTML标记之间移动光标 - In a contenteditable element, move the cursor between HTML tags 如何从可编辑区域开始在光标位置之前获取文本 - How can I get the text right before the cursor position starting from a contenteditable area Contenteditable DIV - 如何确定光标是否位于内容的开头或结尾 - Contenteditable DIV - how can I determine if the cursor is at the start or end of the content 如何检查文本 cursor 是否在 contenteditable div 内? - How can I check if the text cursor is inside of a contenteditable div?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM