是否可以通过编程方式检测< input type = text>中的插入符号位置.元素? [英] Is it possible to programmatically detect the caret position within a <input type=text> element?

查看:51
本文介绍了是否可以通过编程方式检测< input type = text>中的插入符号位置.元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设一个常规的< input type = text> 文本框中包含数据.

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

是否可以(通过JavaScript)检测文本向导在该文本框中的位置?

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

我能够检测到ARROW LEFT或ARROW RIGHT按下事件-但是如何检测光标位置?

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

为什么我需要这个:

Why I need this:

我在这里有一个动态文本框: http://vidasp.net/tinydemos/dynamic-textbox.html
效果很好,但是我要修复两种情况:

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. 当光标位于文本框的开头并且用户按BACKSPACE时
  2. 当光标位于文本框的末尾并且用户按下DELETE时

(在两种情况下,文本框都必须包含数据才能使效果可见.)

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

推荐答案

我已经做了很多工作.以下内容适用于所有主要浏览器(包括IE 6),适用于文本< input> < textarea> 前导和尾随空格(这是许多解决方案(包括a-tools)掉落的地方).此问题中的这段代码有一些背景:IE的document.selection.createRange不包含开头或结尾的空白行

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

作为我编写的jQuery input/textarea选择插件的一部分,您还可以获得以下内容:

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);

这篇关于是否可以通过编程方式检测&lt; input type = text&gt;中的插入符号位置.元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆