通过Jquery

时间:2015-10-14 15:48:46

标签: javascript jquery twitter-bootstrap drop-down-menu

所以我有一个带有几个下拉菜单的导航栏。简而言之,它很难编码到我们的网站中,我们需要找到一种方法来触发这些没有鼠标的下拉菜单。我尝试了其他方法,但截至目前我想使用这个方法:

当盲人用户在菜单中进行选项卡时,我希望下拉菜单在获得焦点时打开。我相信我只需要正确的语法来实际触发它,但我找不到它。我正在使用这个jquery:

$(document).ready(function () {
    $("#testF").on("focus",function() {
        $(this).dropdown("open");
    });
});

我的问题似乎在.navbar-toggle(“open”)中; (语法错误) 我只想弄清楚这个下拉列表是如何打开它的焦点?任何帮助将不胜感激,这里有一些下拉HTML帮助:

<li class="dropdown">                                    
    <a id="testF" href="#" class="dropdown-toggle" data-toggle="dropdown">Courses<span class="caret" /></a>                                       
    <ul class="dropdown-menu" role="menu">
        <li>
             <a href="/home/BbDeepLink" target="_blank">All Courses (click here)</a>
        </li>

CSS:

/* Menu */
    .navbar-default .navbar-nav > li > a{
        color:#222;
    }
    .navbar-default .navbar-nav .open .dropdown-menu > li > a {
        color:#222;
        background-color:#fff;
    }
    .navbar-default .navbar-nav > li > a:hover, .navbar-default .navbar-nav > li > a:focus {
        color:#fff;
        background-color:#5396b1;
    }
    .navbar-default .navbar-nav .open .dropdown-menu > li > a:hover, .navbar-default .navbar-nav .open .dropdown-menu > li > a:focus {
        color:#fff;
        background-color:#5396b1;
    }
    .dropdown-menu{
        padding:0px;
    }
}

@media (min-width: 781px) {
    ul.nav li.dropdown:hover > ul.dropdown-menu {
        display: block;
    }
    /* Fixes: The main menu hover/focus dropdown issue */
    .navbar-default .navbar-nav .open ul  {
        display:none;
    }
    .navbar-default .navbar-nav .open:hover ul {
        display:block;
    }
}

5 个答案:

答案 0 :(得分:2)

$(document).ready(function () {
    $("#testF").on("focus",function() {
        $("#testF").click();
   });
});

答案 1 :(得分:1)

灵感来自:jQuery handing both focus and click on an element

想法是在标签或鼠标点击后检测它是否是焦点。

var lastClick = null;
    $('#testF').mousedown(function (e) {
        lastClick = e.target;
    }).focus(function (e) {
        if (e.target != lastClick) {
            $(this).dropdown("toggle");
        }
        lastClick = null;

    });

希望这是你问题的可接受答案。

答案 2 :(得分:0)

经过反复试验,我达到了我想要的结果(差不多,现在需要模糊,所以它不会保持开放,提示欢迎!),代码如下:

$(document).ready(function () {
        $("#testF").on("focus",function() {
            $(this).next(".dropdown-menu").toggle()

        });
    });

答案 3 :(得分:0)

您可以尝试一下。

$("#testF").select2();
$("#testF").next(".select2").find(".select2-selection").focus(function() {
    $("#testF").select2("open");
});

答案 4 :(得分:-1)

这是我的解决方案:

$("#dropdownMenuButton").on("focus", elements => {
    var element = elements.currentTarget;
    $(element).dropdown("toggle");
})
<!DOCTYPE html>
<html>
    <head lang="en">
        <meta charset="utf8">
        <title>Page Title</title>
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB" crossorigin="anonymous">
    </head>
    <body>
        <main class="container">
            <div class="row">
                <div class="col-xl-12">
                    <h1>Teste de Menu</h1>
                    <div class="dropdown dropright">
                            <button class="btn btn-outline-primary dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                                Menu
                            </button>
                            <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
                                <a class="dropdown-item" href="#">Action</a>
                                <a class="dropdown-item" href="#">Another action</a>
                                <a class="dropdown-item" href="#">Something else here</a>
                            </div>
                    https://stackoverflow.com/questions/33129942/open-a-dropdown-menu-when-it-receives-focus-via-jquery#    </div>
                </div>
            </div>
            
        </main>
        <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
        <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js" integrity="sha384-smHYKdLADwkXOn1EmN1qk/HfnUcbVRZyYmZ4qpPea6sjB/pTJ0euyQp0Mk8ck+5T" crossorigin="anonymous"></script>      
    </body>
</html>