Domanda

Perché nel Codice seguente il focusin Il gestore di eventi non viene chiamato?

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;
}
È stato utile?

Soluzione

Per qualche ragione, non sono positivo perché, .focus() non attiva il focusin evento.

Puoi replicare questo comportamento modificando la linea di messa a fuoco da aggiungere .trigger('focusin').

Quindi il tuo codice Fadein diventa:

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

Puoi testarlo qui: http://jsfiddle.net/yt7jd/

EDIT: come ha detto Jason, puoi anche chiamare il .focusin() metodo invece del .trigger('focusin').

EDIT 2: sembra essere un bug in 1.4.3. È stato registrato con il team jQuery per la correzione: http://bugs.jquery.com/ticket/7340

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top