jquery - Simplemodal - 如何在 onShow 函数中将焦点设置在元素上

标签 jquery focus simplemodal

我以为这会很简单。只是

...
onShow:function(dialog){
   dialog.data.find("input#myInput").focus();
}
...

但似乎不起作用。

与SimpleModal的focus()函数有冲突吗?

我注意到,如果我在 onShow 函数中放置一个alert(),它会在模态可见之前弹出。有没有办法在显示模式后调用函数(即 focus()),但无需用户输入(无需用户触发某种事件)?

此外,当我尝试使用 simplemodal 时

focus:true

与dialog.data =

'<div class="myModal login">'+
    '<div class="modalTitle">Title</div>'+
    '<div class="modalContent">'+
       '<label for="username">Login: </label>'+
       '<input id="username" />'+
       '<br />'+
       '<label for="password">Pass: </label>'+
       '<input id="password" type="password" />'+
       '<br />'+
       '<div class="confirmButton no simplemodal-close">Cancel</div>'+
       '<div class="confirmButton yes">Login</div>'+
    '</div>'+
'</div>'

焦点是包装器(至少我是这么认为),用户必须按 Tab 才能到达第一个输入元素。

任何帮助都会很棒,

格雷格。

最佳答案

试试这个:

onShow:function(dialog){
   setTimeout(function(){
     dialog.data.find("input#myInput").focus();
   },50);
}

希望这有帮助。干杯

关于jquery - Simplemodal - 如何在 onShow 函数中将焦点设置在元素上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5921351/

相关文章:

jquery - IE 卡在 js 错误上

javascript - 如何在 INPUT FIELD 而不是 SPAN 中将变量显示为总计?

apache-flex - 在弹出文本输入控件上设置焦点

css - 显示/隐藏 DIV 不起作用 - 仅限 HTML/CSS

javascript - 如何使 $(this) 选择器聚焦于当前元素?

javascript - 弹出格式化的模式对话框并在用户确认时转发

jquery.simplemodal-1.3.3.js : absolute positioning (vs. 已修复)

javascript - 更改特定 DOM 元素的 src

javascript setTimeout 或 jquery delay - 两者都不适合我

javascript - 使用 PHP 和 jQuery 上传图像时声明新的 formData()