摇动效果杀死焦点

jQuery震动效果似乎杀死了被震动元素的焦点。 例如(参见http://jsfiddle.net/xSNBp/ )

$('input').focus().effect('shake', { times: 3, distance: 2 }, 30);

杀死了焦点。 这是一个错误吗? 我的问题是,当触发震动效果时,我不知道当前聚焦的是什么元素,因此我无法重新聚焦它。 有什么建议?

(如果这是一个错误,我该怎么办?)

我们最近推动了一个提交到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(); } } } 

如果这不是您遇到的问题,请告知我们错误跟踪器。

它发生的原因是当你“封装”或将一个聚焦元素附加到DOM中的其他地方时 – 它会失去焦点。 因此我们必须在createWrapperremoveWrapper添加一个检查以保留焦点。

您可以尝试以下方法。 基本上你只需保存聚焦的元素,这样你就可以在动画完成重新聚焦它。

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

现场演示

这样做怎么样:

 $('input').keypress(function() { var el = $(this); el.effect('shake', { times: 10, distance: 5 }, 30, function() { el.focus() }); }) 

的jsfiddle