为什么在 following code未调用 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/
编辑:正如 Jason 提到的,您还可以调用 .focusin()
方法而不是 .trigger('focusin')
。
编辑 2:这似乎是 1.4.3 中的一个错误。 jQuery 团队已记录该问题以进行修复:http://bugs.jquery.com/ticket/7340
关于javascript - 为什么没有调用 "focusin"事件处理程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4039952/