jQuery表单关注元素创建

寻找我的jQuery代码的一些帮助。

我正在使用simplemodal插件创建一个模态框

我正在设法在元素的点击上创建一个模态框,其中包含一个表单….我希望表单在出现时能够专注于textarea字段,但我不确定如何实现这一点。

这是我目前的代码..

$('#showModal a').live('click',(function(){ // Build Modal Box Container And Add to Page $('
\

Please explain this song.

\
\

\
\
').appendTo('body'); //Set Modal Box Options $("#osx-modal-content").modal({ // OPTIONS SET HERE overlayClose:true, onOpen:OSX.open, onClose:OSX.close }); // I have a hidden form on the page...grab its HTML!! var modalForm = $('#addTmWrapper').html(); // Dynamically build a text area and add to the form... // The text area from my hidden form wont show properly for some reason.....maybe a coldfusion issue var textField= '' textField+= '' // Add hidden form to modal box, add textarea to form.. // Then show it $('#osx-modal-data').html(modalForm ).find('#addTmForm') .prepend(textField) .show(); return false }));

想知道当模态框出现时我怎么能让textarea有焦点?

任何帮助将不胜感激,谢谢

要将焦点放在表单元素上,请使用focus()

 $('#myElement').focus() 

试试这个:

 var aSet = $('selector'); aSet[0].focus(); 

上面的焦点适用于Element而不是jQuery Object。

就在return false;之前return false; ,添加$('#sMessages').focus(); (参见jQuery文档 )。

使用

 $('selector').trigger('focus'); 

代替

 $('selector').focus(); 

$(…)。focus(function(){})绑定一个函数来聚焦不做焦点的事件。)

将它包装在一个timeOut中以使其工作:就像在这里完成的那样

 setTimeout(function(){ $('selector').focus(); }, 0);