admin管理员组文章数量:1417397
I wanted to make modal dialog accessible . I added two hidden focusable elements
<a href="javascript:void(0)" id="dialog-start">Dialog Start </a>
Some focussable Dialog Elements
<a href="javascript:void(0)" id="dialog-end" onblur="onblurevent()">Dialog end</a>
function onblurevent(){
document.getElementById("dialog-start").focus();
}
When ever dialog-end element blur event happens i tried to move focus to dialog-start element calling focus() method but the focus is moving to address bar .dialog start and end anchor tags are hidden by using below style
#dialog-start{
height:1px;
left:-9999px;
overflow:hidden;
position:absolute;
top:0;
width:1px;
}
Iam not sure if anchor styles are the reason or is the only way to make sure focus is inside the dialog is to get list of focusable elments and call focus() method in a keydown event handler on container.
I wanted to make modal dialog accessible . I added two hidden focusable elements
<a href="javascript:void(0)" id="dialog-start">Dialog Start </a>
Some focussable Dialog Elements
<a href="javascript:void(0)" id="dialog-end" onblur="onblurevent()">Dialog end</a>
function onblurevent(){
document.getElementById("dialog-start").focus();
}
When ever dialog-end element blur event happens i tried to move focus to dialog-start element calling focus() method but the focus is moving to address bar .dialog start and end anchor tags are hidden by using below style
#dialog-start{
height:1px;
left:-9999px;
overflow:hidden;
position:absolute;
top:0;
width:1px;
}
Iam not sure if anchor styles are the reason or is the only way to make sure focus is inside the dialog is to get list of focusable elments and call focus() method in a keydown event handler on container.
Share Improve this question edited Nov 23, 2012 at 7:52 Rajani asked Nov 23, 2012 at 7:15 RajaniRajani 131 gold badge1 silver badge4 bronze badges 2- where is the code for the onblurevent() call? It would be useful to have that. – Bruno Commented Nov 23, 2012 at 7:24
- Thanks for helping .I included onblurevent() code and also styles i used for anchor tag . – Rajani Commented Nov 23, 2012 at 7:57
2 Answers
Reset to default 2The problem occurs because you don't handle your keydown event. When you pressing Tab on last link browser automatically switches focus to address bar. So you just need to preventDefault()
default browser behavior if Tab pressed.
The following code will work:
window.onload = function() {
var firstAnchor = document.getElementById("dialog-start"),
lastAnchor = document.getElementById("dialog-end");
function keydownHandler(e) {
var evt = e || window.event;
var keyCode = evt.which || evt.keyCode;
if(keyCode === 9) { // TAB pressed
if(evt.preventDefault) evt.preventDefault();
else evt.returnValue = false;
firstAnchor.focus();
}
}
if(lastAnchor.addEventListener) lastAnchor.addEventListener('keydown', keydownHandler, false);
else if(lastAnchor.attachEvent) lastAnchor.attachEvent('onkeydown', keydownHandler);
}
(note that you dont need onblurevent
function anymore)
$(document).ready(function () {
//set focus on first field in Bootstrap modal when loaded
$("#yourModal").on('shown.bs.modal', function () {
$(this).find('#yourField').focus();
});
});
本文标签: javascripttrapping focus for a modal dialogStack Overflow
版权声明:本文标题:javascript - trapping focus for a modal dialog - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745271275a2650906.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论