質問

なぜ 次のコード focusin イベントハンドラーは呼ばれていませんか?

HTML:

<div id='wrapper'></div>
<div id='button'>Click Here</div>
<div id='output'></div>

JS:

$(function() {
    $('input').live('focusin', function() {
        $('#output').html('focusin'); // Why this not happens ?
    });
    $('#button').click(function() {
        $('#button').hide();
        build_inputs();
    });    
});
function build_inputs() {
    var html = "<input type='text' /> \
                <br /> \
                <input type='text' />";
    $('#wrapper').append(html);
    $('#wrapper').fadeIn(500, function() {
        $('input:first').focus();
    });
}

CSS:

#wrapper {
    display: none;
    background: #aaa;
    width: 170px;
    padding: 20px;
}
役に立ちましたか?

解決

何らかの理由で、私は前向きではありません、 .focus() トリガーしません focusin イベント。

フォーカスラインを変更して追加することで、この動作を複製できます .trigger('focusin').

したがって、フェデインコードは次のようになります。

$('#wrapper').fadeIn(500, function() {
    $('input:first').focus().trigger('focusin');
});

ここでテストできます: http://jsfiddle.net/yt7jd/

編集:ジェイソンが言及したように、あなたも電話することができます .focusin() の代わりに .trigger('focusin').

編集2:1.4.3のバグのようです。修正のためにjqueryチームに記録されています。 http://bugs.jquery.com/ticket/7340

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top