5

ユーザーがonblurイベントを発生させたときに、テキスト入力ボックスの内容を左揃えにする方法を探しています。onblur = "this.value = this.value"で問題を解決するという言及を見てきましたが、これはIEでは機能しません。

明確にするために:この問題は、ユーザーがテキストボックスの幅を超えて入力し、フォーカスを離れたときにInternetExplorerで発生します。ChromeとFirefoxでは、テキストは自動的に左揃えになりますが、IEでは発生しません。

リクエストに応じて、コードを添付しました(IEで表示):

<input type="text" />

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

テキストフィールド:

テキストフィールド

入力後:

入力後

ぼかし後:

ぼかし後

どのように見えるべきか:

どのように見えるべきか

4

3 に答える 3

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);" />

onchangeの代わりにの使用に注意してくださいonblur。これは、select()を使用するときに問題が発生するためonblurです。

于 2013-02-07T18:25:10.270 に答える
1

これが私がモックアップしたjQueryバージョンで、すべての主要なブラウザーで機能します。

$(document).ready(function(){
        $('input[type=text]').change(function(){
            try{//select nothing but side-effect is that text left justifies
                if(/webkit/.test(navigator.userAgent.toLowerCase())){
                    return;//webkit browsers do it automatically
                } else if(this.createTextRange) { //ie
                    var r = this.createTextRange();
                    r.collapse(true);
                    r.select();
                } else if(typeof this.selectionStart !== "undefined") { //firefox
                    this.selectionStart = 0;
                    this.selectionEnd = 0;
                }
            } catch(err) {
            //?? nobody cares ??
            }
        });
});
于 2014-02-18T15:50:04.560 に答える
0

そのフィールドにクラスを設定してから、htmlのonBlurで関数を起動します。たとえばonBlur="left()"、JavaScriptで、次のようにクラスを変更するだけです.setAttribute。追加のボーナスとして、そのクラスの他のプロパティを次のように設定することもできます。良い。

私は関連するコードを投稿するという概念をサポートしていますが

于 2013-02-07T15:59:19.287 に答える