本文介绍了为什么要“聚焦"?事件处理程序不被调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么在以下代码中,未调用focusin事件处理程序?

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

推荐答案

出于某种原因,我不太肯定为什么.focus()不会触发focusin事件.

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

您可以通过更改焦点线以添加.trigger('focusin')来复制此行为.

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

因此您的fadeIn代码变为:

so your fadeIn code becomes:

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

您可以在此处进行测试: http://jsfiddle.net/yt7Jd/

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

如Jason所述,您还可以调用.focusin()方法而不是.trigger('focusin').

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

它似乎是1.4.3中的错误.它已记录在jQuery团队中,用于修复: http://bugs.jquery.com/ticket/7340

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

这篇关于为什么要“聚焦"?事件处理程序不被调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-15 02:46