Setting (or Reading) value of Cursor/Caret in HTML TextArea

杀马特。学长 韩版系。学妹 提交于 2019-12-01 14:39:31

I worked this out by piecing together a few snippets and ideas from around here. Here is how I solved this one:

  1. I placed each section of text I wanted to make draggable into a span with a class of 'draggable_text'

  2. used jquery (in document ready function) to auto-select/deselect the text inside the span each time mouseover/mouseout fired.

.. here is the code:

$(document).ready (

    $('.draggable_text').mouseover(
        function () {
          selectNode(this);
        }
    );

    $('.draggable_text').mouseout(
        function () {
          clearSelection(this);
        }
    );

);

function selectNode (node) {
    var selection, range, doc, win;
    if ( (doc = node.ownerDocument) 
          && (win = doc.defaultView) 
          && typeof win.getSelection != 'undefined' 
          && typeof doc.createRange != 'undefined' 
          && (selection = window.getSelection()) 
          && typeof selection.removeAllRanges != 'undefined'
        ) {
        range = doc.createRange();
        range.selectNode(node);
        selection.removeAllRanges();
        selection.addRange(range);
    } else if ( document.body 
                && typeof document.body.createTextRange != 'undefined' 
                && (range = document.body.createTextRange()) ) {
        range.moveToElementText(node);
        range.select();
    }
}

function clearSelection () {
    if (document.selection) {
        document.selection.empty();
    } else if (window.getSelection) {
        window.getSelection().removeAllRanges();
    }
}

Interestingly, using this methodology, no special drag & drop mechanisms need to be implemented - it seems that the browser automatically assumes when you drag selected text into a text area, that you want to copy it there. Perfect!

I tested this on FF3, IE6, and IE7 (WinXP and Vista). The drag/drop graphic was slightly different on each, but all worked.

I recommend starting out with reading up on the Range object in JS, see http://www.quirksmode.org/dom/range_intro.html

Since you mention you're using jQuery, I would recommend you to give a look to the FieldSelection plugin. That plugin will help you to get information about where the cursor is currently placed.

For setting the cursor position, I use the following functions:

function setSelectionRange(input, selectionStart, selectionEnd) {
  if (input.setSelectionRange) {
    input.focus();
    input.setSelectionRange(selectionStart, selectionEnd);
  }
  else if (input.createTextRange) {
    var range = input.createTextRange();
    range.collapse(true);
    range.moveEnd('character', selectionEnd);
    range.moveStart('character', selectionStart);
    range.select();
  }
}

function setCaretToPos (input, pos) {
  setSelectionRange(input, pos, pos);
}

The you can use the setCaretToPos function like this:

setCaretToPos(document.getElementById("textareaId"), 4);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!