开发者

Is it possible to programmatically detect the caret position within a <input type=text> element?

Assuming a regular <input type=text> text-box with data in it.

Is it possible to detect (via JavaScript) the position of the text-coursor inside that text-box?

I am able to detect an ARROW LEFT or ARROW RIGHT keydown event - but how to detect the cursor location?

Why I need this:

I have a dynamic text-box here: http://vidasp.net/tinydemos/dynamic-textbox.html

It works great, however there are two scenarios that I would like to fix:

  1. when the cursor is at the beginning of the text-box and the user presses BACKSPACE
  2. when the cursor is at the end of the text-box and the user pr开发者_如何学编程esses DELETE

(In both cases, the text-box must contain data for the effect to be observable.)


I've done quite a lot of work on this. The following works in all major browsers (including IE 6) for text <input>s and <textarea>s and will work in all situations, including when there are leading and trailing spaces (which is where many solutions, including a-tools, fall down). There's some background to this code in this question: IE's document.selection.createRange doesn't include leading or trailing blank lines

You can also get the following as part of a jQuery input/textarea selection plug-in I've written that is as yet undocumented: http://code.google.com/p/rangyinputs/

function getInputSelection(el) {
    var start = 0, end = 0, normalizedValue, range,
        textInputRange, len, endRange;

    if (typeof el.selectionStart == "number" && typeof el.selectionEnd == "number") {
        start = el.selectionStart;
        end = el.selectionEnd;
    } else {
        range = document.selection.createRange();

        if (range && range.parentElement() == el) {
            len = el.value.length;
            normalizedValue = el.value.replace(/\r\n/g, "\n");

            // Create a working TextRange that lives only in the input
            textInputRange = el.createTextRange();
            textInputRange.moveToBookmark(range.getBookmark());

            // Check if the start and end of the selection are at the very end
            // of the input, since moveStart/moveEnd doesn't return what we want
            // in those cases
            endRange = el.createTextRange();
            endRange.collapse(false);

            if (textInputRange.compareEndPoints("StartToEnd", endRange) > -1) {
                start = end = len;
            } else {
                start = -textInputRange.moveStart("character", -len);
                start += normalizedValue.slice(0, start).split("\n").length - 1;

                if (textInputRange.compareEndPoints("EndToEnd", endRange) > -1) {
                    end = len;
                } else {
                    end = -textInputRange.moveEnd("character", -len);
                    end += normalizedValue.slice(0, end).split("\n").length - 1;
                }
            }
        }
    }

    return {
        start: start,
        end: end
    };
}

var el = document.getElementById("your_input");
var sel = getInputSelection(el);
alert(sel.start + ", " + sel.end);


yes it's possible.

It's even simpler if you use

http://plugins.jquery.com/project/a-tools

Good Luck :)

edit: please note that the cursor is msot often reffered to as "caret", just FYI ;)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜