光标移动到文本字段的开头

发布于 2024-10-04 02:47:47 字数 1587 浏览 0 评论 0原文

我在使用 IE8(且仅 IE)时遇到了一些问题,当我聚焦其中包含文本的输入字段时,光标会移动到该字段的开头。我正在尝试将光标设置在末尾。我用谷歌搜索并找到了以下解决方案:

function setSelectionRange(input, selectionStart, selectionEnd) {
    input = document.getElementsByTagName("input")[0];
    if (input.createTextRange) {
        var range = input.createTextRange();
        range.collapse(true);
        range.moveEnd('character', selectionEnd);
        range.moveStart('character', selectionStart);
        range.select();
    }
}

这里的“输入”只是一个类中的输入字段(var inputElement = this.input;)。问题是“setSelectionRange”和“createTextRange”。我做错了什么吗? createTextRange 是否仅为 TextArea 定义?

@Edit:好吧,在将输入更改为 document.getElementsByTagName("input")[0]; 后,我似乎使用了类似“两个对象”的 js 输入和 jquery 输入;我可以转到“createTextRange”分支,但它仍然不会改变光标的位置。

@Edit2:我对代码做了一些更改,现在我从文档中获取输入并进入 if 分支。但随后浏览器向我显示:

Unexpected call to method or property access.

在这一行 var range = input.createTextRange();

@Edit3:回答 James 的问题。我有一个类,在该类中,整个事物与具有输入的 jsp 相关联。在这个类中,我为与 jsp inputElement.focus(onInputFocus) 的输入关联的字段设置了一个焦点处理程序,然后我有这样的东西:

function onInputFocus() {
    isFocused = true;
    valueDivElement.hide();
    labelElement.html(labelFocus);
    if (currentData.selectedEntityCode) {
        inputElement.val(currentData.selectedEntityCode);
        inputElement.attr('title', currentData.selectedEntityCode);
    } else {

    }

    var input = document.getElementsByTagName("input")[0];
    input.value = input.value;
}

整个类显然要大得多,这是不是我的代码,但我猜这是最后执行的事情。

I have a certain problem with IE8 (and only IE), when I focus an input field which has text in it the cursor moves to the beginning of that field. I'm trying to set the cursor at the end. I've googled around and found the following solution:

function setSelectionRange(input, selectionStart, selectionEnd) {
    input = document.getElementsByTagName("input")[0];
    if (input.createTextRange) {
        var range = input.createTextRange();
        range.collapse(true);
        range.moveEnd('character', selectionEnd);
        range.moveStart('character', selectionStart);
        range.select();
    }
}

"Input" here is simply an input field which is in a class (var inputElement = this.input;). The problem is that both "setSelectionRange" and "createTextRange". Am I doing something wrong? Is createTextRange defined only for TextArea?

@Edit: well it appears I was using something like "two objects" a js input and a jquery input, after changing input to document.getElementsByTagName("input")[0]; I am able to go to "createTextRange" branch but it still doesn't change the position of the cursor.

@Edit2: I changed the code a bit, now I'm getting the input from the document and it enters the if branch. But then the browser shows me:

Unexpected call to method or property access.

On this line var range = input.createTextRange();

@Edit3: To answer James' question. I have a class and in that class, the whole thing is associated with a jsp which has an input. In this class I set a focus handler for the field which is associated with the input form the jsp inputElement.focus(onInputFocus) then I have something like this:

function onInputFocus() {
    isFocused = true;
    valueDivElement.hide();
    labelElement.html(labelFocus);
    if (currentData.selectedEntityCode) {
        inputElement.val(currentData.selectedEntityCode);
        inputElement.attr('title', currentData.selectedEntityCode);
    } else {

    }

    var input = document.getElementsByTagName("input")[0];
    input.value = input.value;
}

The whole class is obviously much larger and this is not my code but I'm guessing this is the last thing that's being executed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

唠甜嗑 2024-10-11 02:47:47

抱歉,我在这里遗漏了什么吗?您是否尝试将光标设置到末尾,或突出显示范围?你的问题似乎暗示着前者,但解决方案实现了后者。

如果你只是想将光标设置到末尾,那么上面的函数就没有必要了。设置焦点后,只需执行以下操作:

input.value = input.value; //assumes input is a DOM element.

请参阅此 JSFiddle: http://jsfiddle.net/7vdv6/

编辑:

据我所知,有一大堆东西可能会将焦点从您的输入上转移开:

isFocused = true;
valueDivElement.hide();
labelElement.html(labelFocus);
if (currentData.selectedEntityCode) {
    inputElement.val(currentData.selectedEntityCode);
    inputElement.attr('title', currentData.selectedEntityCode);
} else {

}

如果您在最后两行之间添加 focus() 会发生什么:

var input = document.getElementsByTagName("input")[0];
input.focus();
input.value = input.value;

您'我们还非常混乱地混合了 jQuery 和标准 JavaScript。例如,当您可以使用 $("input:first") 时,为什么要使用 document.getElementsByTagName("input")[0]

Sorry am I missing something here? Are you trying to set the cursor to the end, or highlight the range? You question would seem to imply the former, but the solution achieves the latter.

If you just want to set the cursor to the end, then the above function is not necessary. Simply do the following once focus has been set:

input.value = input.value; //assumes input is a DOM element.

See this JSFiddle: http://jsfiddle.net/7vdv6/

EDIT:

As far as i can seen there is a whole bunch of stuff that might be diverting focus away from your input:

isFocused = true;
valueDivElement.hide();
labelElement.html(labelFocus);
if (currentData.selectedEntityCode) {
    inputElement.val(currentData.selectedEntityCode);
    inputElement.attr('title', currentData.selectedEntityCode);
} else {

}

What happens if you add a focus() in between your last two lines:

var input = document.getElementsByTagName("input")[0];
input.focus();
input.value = input.value;

You've also got quite a messy mix of jQuery and standard JavaScript. For example, why are you using document.getElementsByTagName("input")[0] when you could use $("input:first")??

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文