刚刚开始使用 bootstrap,这真是太棒了。
我正在尝试解决这个问题。我在模式窗口内有一个用于反馈的文本区域。效果很好。但我希望当您单击按钮激活模式时,焦点位于文本区域上。而且我似乎无法让它发挥作用。
这是一个小提琴:http://jsfiddle.net/denislexic/5JN9A/4/ http://jsfiddle.net/denislexic/5JN9A/4/
这是代码:
<a class="btn testBtn" data-toggle="modal" href="#myModal" >Launch Modal</a>
<div class="modal hide" id="myModal">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h3>Modal header</h3>
</div>
<div class="modal-body">
<textarea id="textareaID"></textarea>
</div>
<div class="modal-footer">
<a href="#" class="btn" data-dismiss="modal">Close</a>
<a href="#" class="btn btn-primary">Save changes</a>
</div>
</div>
这是JS
$('.testBtn').on('click',function(){
$('#textareaID').focus();
});
恢复当我单击“启动模式”时,我希望显示模式并将焦点放在文本区域上。
谢谢你的帮助。
它不起作用,因为当您单击按钮时,模式尚未加载。您需要将焦点放在一个事件上,然后转到引导模态页面 http://getbootstrap.com/2.3.2/javascript.html#modals我们看到了这个事件shown
, that is fired when the modal has been made visible to the user (will wait for css transitions to complete)
。这正是我们想要的。
尝试这个:
$('#myModal').on('shown', function () {
$('#textareaID').focus();
})
这是你的小提琴更新:http://jsfiddle.net/5JN9A/5/ http://jsfiddle.net/5JN9A/5/
Update:
As @MrDBA https://stackoverflow.com/users/488293/mrdba注意,在 Bootstrap 3 中事件名称是changed http://getbootstrap.com/javascript/#modals-usage to shown.bs.modal
。因此,对于 Bootstrap 3 使用:
$('#myModal').on('shown.bs.modal', function () {
$('#textareaID').focus();
})
Bootstrap 3 的新小提琴:http://jsfiddle.net/WV5e7/ http://jsfiddle.net/WV5e7/
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)