Jquery 将光标放在文本输入的左侧

Jquery 将光标放在文本输入的左侧,jquery,html,css,input,Jquery,Html,Css,Input,可能重复: 聚焦文本输入时,光标从右侧文本的末尾开始。如何使光标位于焦点左侧 我见过使用“setCursor”的例子,但我得到一个错误,setCursor没有定义 <input type="text" class="myInput"> Tabbing into this should put my cursor at the left! </input> 试试这个 <input type="text" class="myInput" value="Tab

可能重复:

聚焦文本输入时,光标从右侧文本的末尾开始。如何使光标位于焦点左侧

我见过使用“setCursor”的例子,但我得到一个错误,setCursor没有定义

<input type="text" class="myInput">
    Tabbing into this should put my cursor at the left!
</input>
试试这个

<input type="text" class="myInput" value="Tabbing into this should put my cursor at the left!" />

(function(){
        jQuery.fn.setSelection = function(selectionStart, selectionEnd) {
            if(this.length == 0) return this;
            input = this[0];

            if (input.createTextRange) {
                var range = input.createTextRange();
                range.collapse(true);
                range.moveEnd('character', selectionEnd);
                range.moveStart('character', selectionStart);
                range.select();
            } else if (input.setSelectionRange) {
                input.focus();
                input.setSelectionRange(selectionStart, selectionEnd);
            }

            return this;
        }
        jQuery.fn.setCursorPosition = function(position){
            if(this.length == 0) return this;
            return $(this).setSelection(position, position);
        }
        $(".myInput").focus(function(){
            $(this).setCursorPosition(0);
        });
    })();

(功能(){
jQuery.fn.setSelection=函数(selectionStart,selectionEnd){
如果(this.length==0),则返回该值;
输入=此[0];
if(input.createTextRange){
var range=input.createTextRange();
范围。塌陷(真);
range.moveEnd('character',selectionEnd);
range.moveStart('character',selectionStart);
range.select();
}else if(输入设置选择范围){
input.focus();
input.setSelectionRange(selectionStart,selectionEnd);
}
归还这个;
}
jQuery.fn.setCursorPosition=函数(位置){
如果(this.length==0),则返回该值;
返回$(此).setSelection(位置,位置);
}
$(“.myInput”).focus(函数(){
$(此).setCursorPosition(0);
});
})();

希望现在就可以工作了

很接近了,但是这个问题的答案是从2009年开始的,所以现在可能有更干净的方法来处理它:)“当一个文本输入被聚焦时,光标从右边文本的末尾开始”-如果标签插入,它通常不会选择输入中的所有文本,或者如果使用鼠标,将光标放在你点击的地方?@DonnyP实际上我认为这方面没有任何变化,即使使用HTML5。
<input type="text" class="myInput" value="Tabbing into this should put my cursor at the left!" />

(function(){
        jQuery.fn.setSelection = function(selectionStart, selectionEnd) {
            if(this.length == 0) return this;
            input = this[0];

            if (input.createTextRange) {
                var range = input.createTextRange();
                range.collapse(true);
                range.moveEnd('character', selectionEnd);
                range.moveStart('character', selectionStart);
                range.select();
            } else if (input.setSelectionRange) {
                input.focus();
                input.setSelectionRange(selectionStart, selectionEnd);
            }

            return this;
        }
        jQuery.fn.setCursorPosition = function(position){
            if(this.length == 0) return this;
            return $(this).setSelection(position, position);
        }
        $(".myInput").focus(function(){
            $(this).setCursorPosition(0);
        });
    })();