jQuery抖动效果似乎会杀死被抖动的元素的焦点。例如(参见http://jsfiddle.net/xSNBp/)
$('input').focus().effect('shake', { times: 3, distance: 2 }, 30);
扼杀了焦点。这是一个bug吗?我的问题是,当抖动效果被触发时,我不知道哪个元素当前被聚焦,所以我不能重新聚焦它。有什么建议吗?
(如果这是一个bug,我应该怎么做?)
发布于 2011-08-31 04:45:21
我们最近将a commit升级到了master / 1.8,以修复带有一些效果的焦点问题。你有没有尝试过使用git中的jQuery UI版本?甚至是UI 1.8.16?
请参阅http://bugs.jqueryui.com/ticket/7595,了解需要该修复的错误。
以下是一个解决方法,可以在较早的1.8代码中为您修复此问题:
function doShake( elem, opts, duration ) {
var active = document.activeElement;
elem.effect( "shake", opts, duration, fixFocus );
fixFocus();
function fixFocus() {
if ( active === elem[0] || $.contains( elem[0], active ) ) {
$( active ).focus();
}
}
}
如果这不是您遇到的问题,请在bug跟踪器上让我们知道。
发生这种情况的原因是,当您将一个焦点元素“包装”或附加到DOM中的其他位置时,它就失去了焦点。因此,我们必须在createWrapper
和removeWrapper
中添加一个检查以保持焦点。
发布于 2011-08-30 22:12:02
您可以尝试以下操作。基本上,您只需保存放入焦点的元素,以便您可以在动画完成后重新聚焦它。
var $focusElement;
$(":input").focus(function () {
$focusElement = $(this);
});
// focus some random element, will be saved in the function above
$('#textTwo').focus();
$('input').effect('shake', { times: 3, distance: 2 }, 30,
function(){
// Refocus the element
$focusElement.focus();
});
发布于 2011-08-31 04:59:50
这样做如何:
$('input').keypress(function() {
var el = $(this);
el.effect('shake', {
times: 10,
distance: 5
}, 30, function() {
el.focus()
});
})
jsFiddle
https://stackoverflow.com/questions/7244558
复制相似问题