左对齐文本输入onblur(IE 8和9)

Jon*_*neh 4 html javascript textinput

我正在寻找一种方法,当用户触发onb​​lur事件时左对齐文本输入框的内容.我已经看到用onblur ="this.value = this.value"解决问题的提及,但这在IE中不起作用.

澄清:当用户键入超出文本框宽度然后离开焦点时,Internet Explorer中会出现此问题.在Chrome和Firefox中,文本将自动保持对齐,但这不会发生在IE中.

由于要求,我附上了代码(在IE中查看):

<input type="text" />
Run Code Online (Sandbox Code Playgroud)

http://jsfiddle.net/t3hjonneh/FZJjW/

文本域:

文本域

打字后:

打字后

模糊之后:

模糊之后

应该如何看待:

应该怎么样

Tee*_*emu 5

这是一个黑客而不是一个正确的解决方案,但不知何故它的工作原理:

function blurred(elem) {
    var range;
    // IE only, probably not the best check since it will run in other browsers
    // if they ever implement this feature
    if (elem.createTextRange) {
        range = elem.createTextRange();
        range.collapse(true);
        range.select();
    }
}

<input type="text" onchange="blurred(this);" />
Run Code Online (Sandbox Code Playgroud)

注意使用onchange而不是onblur.这是因为select()使用时会引起一些麻烦onblur.