0

フォーカスがあるときにJS関数を呼び出すテキスト入力がいくつかあります。基本的に、この関数はこのフィールドの値を変更します。そうすると、IEではカーソルが入力の左端に移​​動します。これはFirefoxでは起こりません。そもそも置いたところにとどまります。

<input maxlength="5"  type="text" onFocus=\"changeValueOnFocus(this);\">";

function changeValueOnFocus(myInput){
  myInput.value = 1234;
}

これを回避する方法はありますか?ありがとう!

4

2 に答える 2

2

onfocusを使用する代わりにonfocusin、コードを機能させることができます。

編集

focusinFirefoxには何もないことに気づきました。したがって、より重いものが必要です。

スクリプト:

function changeValueOnFocus (e, elm) {
        elm = elm || this;
        elm.value = 1234;
        return;  
}

window.onload = function () {
    if (window.onfocusin === undefined) {
        document.getElementById('someinput').addEventListener('focus', changeValueOnFocus, false);
    }
    return;
}

そして、inputあなたのために必要になりますid

<input id="someinput" maxlength="5" onfocusin="changeValueOnFocus(event, this);" type="text" />

現在、これはクロスブラウザーソリューションであるはずです。

于 2012-11-22T17:25:24.720 に答える
1

カーソルをテキストボックスの最後に移動するには、関数を次のように変更します。

     function changeValueOnFocus(myInput) {
     myInput.value = 1234;

     //use this function to select text but in this case just to move cursor to the end 

         myInput.setSelectionRange(myInput.value.length, myInput.value.length);

 }
于 2012-11-22T17:09:22.980 に答える