Why the “focusin” event handler isn't called?

半城伤御伤魂 提交于 2020-01-06 05:06:07

问题


Why in the following code the focusin event handler isn't called ?

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;
}

回答1:


For some reason, I'm not positive why, .focus() doesn't trigger the focusin event.

You can replicate this behaviour by changing the focus line to add .trigger('focusin').

so your fadeIn code becomes:

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

You can test it here: http://jsfiddle.net/yt7Jd/

EDIT: As Jason mentioned, you can also call the .focusin() method instead of the .trigger('focusin').

EDIT 2: It appears to be a bug in 1.4.3. It has been logged with the jQuery team for fixing: http://bugs.jquery.com/ticket/7340



来源:https://stackoverflow.com/questions/4039952/why-the-focusin-event-handler-isnt-called

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!