IE:光标跳转到输入开始

Gil*_*ian 5 html javascript jquery internet-explorer

在文本输入中,如果文本存在,并且单击以添加更多,则光标会自动跳转到开头.它似乎只发生在IE中.

我已经谷歌搜索了一段时间,并尝试了我发现的大量示例,但似乎没有任何工作.不幸的是,输入是用jsp编写的,我无法控制,我认为我遇到这个问题的唯一原因是因为有一些javascript硬编码在那里.

<input type="text" id="simpleSearchString" class="text" onfocus="javascript:this.value=this.value.replace(/^\s+|\s+$/g,'')" onblur="javascript:this.value=this.value.replace(/^\s+|\s+$/g,'')" value="" maxlength="255" name="searchCriteria.simpleSearchString">
Run Code Online (Sandbox Code Playgroud)

我想要做的就是删除这个跳转.

我认为添加以下代码会有所帮助,但似乎没有做任何事情:

jQuery("input#simpleSearchString").focus();
Run Code Online (Sandbox Code Playgroud)

我不是100%肯定开发人员试图通过'javascript:this ... etc'来实现,但也许我可以找到一种方法来删除它.

Ale*_* K. 3

您可以终止那些内联事件并插入您自己的事件;

var el = $("#simpleSearchString");
el[0].onfocus = el[0].onblur = null;

$(el).on("focus blur", function(e) {
    this.value = $.trim(this.value);    
    if ((e.type === "focus") && this.createTextRange) {
        var r = this.createTextRange();
        r.moveStart("character", this.value.length);
        r.select();
    }
});
Run Code Online (Sandbox Code Playgroud)

(这假设它是唯一需要您重新定位插入符号的 IE)