为什么在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;
}发布于 2010-10-28 13:22:57
出于某些原因,我不确定为什么.focus()不会触发focusin事件。
您可以通过将焦点行更改为添加.trigger('focusin')来复制此行为。
所以你的fadeIn代码变成了:
$('#wrapper').fadeIn(500, function() {
$('input:first').focus().trigger('focusin');
});你可以在这里测试它:http://jsfiddle.net/yt7Jd/
EDIT:正如Jason提到的,您还可以调用.focusin()方法而不是.trigger('focusin')。
编辑2:它似乎是1.4.3中的一个错误。它已经被jQuery团队记录下来进行修复:http://bugs.jquery.com/ticket/7340
https://stackoverflow.com/questions/4039952
复制相似问题