javascript - 如何在对话框中锁定焦点?

标签 javascript jquery

我想将焦点锁定在 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/

相关文章:

javascript - 在浏览器中调用 javascript 函数时指定的强制转换无效

javascript - 在输入类型提交按钮上调用 C# 方法

Javascript eval 在复杂的 json 数组上失败

javascript - 如何检查值是否是数字

javascript - 使用 firebase 并使用动态键名保存

javascript - 如何对 Meteor 集合执行查找和排序(使用 Coffeescript)

jquery - 如何修改Google饼图pieSliceText定位

javascript - jQuery 中的函数变量

jquery - 动画响应式 Svg-Map

javascript - 查找线条并更改背景颜色