简体   繁体   English

是否可以以编程方式检测a中的插入位置 <input type=text> 元件?

[英]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. 假设一个常规的<input type=text>文本框,其中包含数据。

Is it possible to detect (via JavaScript) the position of the text-coursor inside that text-box? 是否可以检测(通过JavaScript)文本框内文本方块的位置?

I am able to detect an ARROW LEFT or ARROW RIGHT keydown event - but how to detect the cursor location? 我能够检测到ARROW LEFT或ARROW RIGHT keydown事件 - 但是如何检测光标位置?

Why I need this: 为什么我需要这个:

I have a dynamic text-box here: http://vidasp.net/tinydemos/dynamic-textbox.html 我在这里有一个动态文本框: 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 当光标位于文本框的开头并且用户按下BACKSPACE时
  2. when the cursor is at the end of the text-box and the user presses DELETE 当光标位于文本框的末尾并且用户按下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). 以下适用于所有主要浏览器(包括IE 6)的文本<input><textarea>并且适用于所有情况,包括有前导和尾随空格(这是许多解决方案,包括a-tools,摔倒)。 There's some background to this code in this question: IE's document.selection.createRange doesn't include leading or trailing blank lines 这个代码在这个问题中有一些背景知识: IE的document.selection.createRange不包括前导或尾随空白行

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/ 您还可以将以下内容作为我编写的jQuery输入/ textarea选择插件的一部分,该插件尚未记录: 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 http://plugins.jquery.com/project/a-tools

Good Luck :) 祝好运 :)

edit: please note that the cursor is msot often reffered to as "caret", just FYI ;) 编辑:请注意光标是msot经常被称为“插入符号”,只是FYI;)

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

相关问题 如何在 input type=“number” 元素中获取插入符号 position? - How to get the caret position inside input type=“number” element? 在文本字段内移动插入符号位置 - Moving caret position within text field 如何获取HTML输入元素中光标/插入符号的位置? - How can I get the position of the cursor/caret within an HTML input element? 如何使用javascript在插入符号位置键入文本? - How to type text at the caret position using javascript? 检测输入(type = text)元素中的文本是否超出FireFox中的边界 - Detect if text in an input (type=text) element exceeds bounds in FireFox 在文本输入中将插入位置保持在可见位置 - firefox行为异常 - Keeping caret position in a visible position in text input - firefox misbehaves 是否可以根据文本区域中的插入位置选择元素? - Is it possible to select an element based off of caret position in a textarea? Javascript获取输入文字或文本区域中插入符号的绝对位置(以像素为单位) - Javascript get the absolute position (in px) of a caret in a input text or textarea 是否可以将鼠标单击调用事件()到<input type = text>元素? - Is it possible to dispatchEvent() a mouse click to a <input type=text> element? 在JavaScript中获取具有光标(尖号)的文本输入元素 - Get text input element having cursor (caret) in JavaScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM