光标移动到文本字段的开头 [英] Cursor moves to the beginning of a text field

查看:167
本文介绍了光标移动到文本字段的开头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对IE8(只有IE)有一定的问题,当我对一个有文本的输入字段进行聚焦时,光标移动到该字段的开头。我正在尝试将光标设置在最后。我用Google搜索并找到了以下解决方案:

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

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

"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:在将输入更改为文档后,似乎我使用的是两个对象,js输入和jquery输入.getElementsByTagName( 输入)[0];我可以去createTextRange分支,但它仍然不会改变光标的位置。

@ 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:我改变了一下代码,现在我从文档中获取输入并进入if分支。但随后浏览器显示:

@ 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.

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

@ Edit3:回答詹姆斯的问题。我有一个类,在那个类中,整个事物与一个有输入的jsp相关联。在这个类中,我为字段设置了一个焦点处理程序,它与输入形式jsp inputElement.focus(onInputFocus)相关联,然后我有这样的事情:

@ 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.

推荐答案

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

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.

看到这个JSFiddle: http://jsfiddle.net/7vdv6/

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

编辑:

据我所知,有很多东西可能会把注意力转移到你的输入上:

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;

你也有很混乱的jQuery和标准JavaScript。例如,为什么你使用 document.getElementsByTagName(input)[0] 当你可以使用 $(input:first) ??

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")??

这篇关于光标移动到文本字段的开头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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