Bootstrap模态焦点问题

时间:2018-03-13 19:25:49

标签: javascript jquery html twitter-bootstrap-3 bootstrap-modal

我有引导模式,其中我有桌子,我想通过按键向上关注行,下面的按键是我的代码在js但焦点不在IE中工作

var _checkKey = function (e) {
    var event = window.event ? window.event : e;
    if (event.keyCode === 13)  // the enter key code
    {
        $("tr:focus").click();
        event.preventDefault();
        return;
    }

    if (event.keyCode === 40) { //down
        event.preventDefault();

        var idx = $("tr:focus").attr("tabindex");
        if (!idx)
            idx = 0;
        else
            idx++;

        $("tr[tabindex=" + idx + "]").focus();
    }
    if (event.keyCode === 38) { //up
        event.preventDefault();

        var idx = $("tr:focus").attr("tabindex");
        idx--;

        $("tr[tabindex=" + idx + "]").focus();
    }
}

此函数通过keydown事件调用

bellow是bootstrap模式的HTML代码,

相同的代码在chrome和IE 11中工作,但如果它在bootstrap模型中则不起作用



<div class="modal fade in" id="commonModal" role="dialog" aria-hidden="false" style="display: block;">
    <div class="modal-dialog modal-lg">
        <div class="modal-content visible-o-flow">
            <div class="modal-header">
                <h3 class="modal-title f-l" id="Title">Header</h3>
                <button class="close w-a m-0 f-r" aria-label="Close" type="button" data-dismiss="modal">
                    <span aria-hidden="true">×</span>
                </button>
            </div>
            <div class="modal-body f-w" id="miscDiv">
                <form class="f-h" id="msform">
                    <div class="col-md-12 p-0">
                        <div id="lookupDiv" style="left: 191.09px; top: 153.68px; width: 538.4px; display: block; position: absolute; z-index: 9000;">
                            <table class="table table-responsive table-fixed table-hover">
                                <thead>
                                    <tr>
                                        <th class="col-md-3">Code</th>
                                        <th class="col-md-9">Description</th>
                                    </tr>
                                </thead>
                                <tbody id="lookupTable">
                                    <tr tabindex="0" style="cursor: pointer;"><td>729</td><td>SAMOA</td></tr>
                                    <tr tabindex="1" style="cursor: pointer;"><td>209</td><td>AFGHANISTAN</td></tr>
                                    <tr tabindex="2" style="cursor: pointer;"><td>265</td><td>AZERBAIJAN</td></tr>
                                    <tr tabindex="3" style="cursor: pointer;"><td>201</td><td>IRAN (ISLAMIC REPUBLIC OF)</td></tr>
                                    <tr tabindex="4" style="cursor: pointer;"><td>235</td><td>NEPAL</td></tr>
                                    <tr tabindex="5" style="cursor: pointer;"><td>645</td><td>PARAGUAY</td></tr>
                                    <tr tabindex="6" style="cursor: pointer;"><td>347</td><td>SIERRA LEONE</td></tr>
                                    <tr tabindex="7" style="cursor: pointer;"><td>267</td><td>TAJIKISTAN</td></tr>
                                    <tr tabindex="8" style="cursor: pointer;"><td>233</td><td>VIET NAM</td></tr>
                                    <tr tabindex="9" style="cursor: pointer;"><td>309</td><td>CAMEROON</td></tr>
                                    <tr tabindex="10" style="cursor: pointer;"><td>727</td><td>MARSHALL ISLANDS</td></tr>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

尝试将其放在var事件行之后:

var event = window.event ? window.event : e;
var kCode = event.keyCode || event.which;//for IE, FireFox and Chrome
if (kCode === 13)...

至少对我来说,在Chrome和FireFox中工作

答案 1 :(得分:-1)

我有同样的问题,但没有得到任何宗教解决方案,我经历了这个

我把这段代码放在模态点击事件上。

setTimeout(()=>{
    document.getElementById("id").focus();
},9000);

//id is the name of element where you want to put your focus