我想将焦点锁定在 TAB 键按下上。这样它就应该保留在我的对话框中,直到我单击关闭按钮。下面提到了我的 JS 代码,但它不起作用。谁能帮我解决这个问题?
$(function () {
$("#confirmSubmit").keydown(function(e){
if (e.which == 9 ) { //keycode for TAB
e.preventDefault(); //stops the default behavior of moving focus to the back page
$("#confirm").focus(); //moves focus to your first input button
}
});
});
对话代码:
// code for dialog
<div style="display: none">
<div aria-live="assertive" aria-describedby="Contentdiv"
role="dialog" id="completeReservationMain" >
<div tabindex="-1" id="Contentdiv">
<div id="CompleteReservationContent">
<h2 tabindex="-1" class="help-layer-heading">
Print </h2>
<div tabindex="-1" class="check-in-complete-help">
Are you sure to Submit?
</div>
<div class="center">
<span class="Button" id="Span1"><span class="ButtonInner">
<form method="get" target="_blank" action="/Print.aspx">
<input type="submit" id="confirm" value="Print">
</form>
</span></span><span class="Button " id="Span2">
<span class="ButtonInner">
<input type="submit" title="Submit" id="confirmSubmit" value="Continue Submit">
</span></span>
</div>
</div>
</div>
主要问题描述:我创建了一个具有“对话框”类型 Angular 色的 div 我想将焦点锁定在对话框内的 Tab 键按下上。该对话框中已经添加了关闭按钮。目前,一旦对话框打开,我的焦点就会出现在第一个输入按钮上,然后在 TAB 键上按下,我的焦点就会移动到该对话框内的关闭按钮。 现在,当我第三次按 TAB 键时,焦点将移至后页的输入元素。这意味着焦点不在该对话之外。 我怎样才能将我的焦点锁定在对话框中,以便在对话框关闭之前它不会移出它
最佳答案
最后相同的代码对我有用:
$(function () {
$("#confirmSubmit").keydown(function(e){
if (e.which == 9 ) { //keycode for TAB
e.preventDefault(); //stops the default behavior of moving focus to the back page
$("#confirm").focus(); //moves focus to your first input button
}
});
});
关于javascript - 如何在对话框中锁定焦点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31469525/