Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 通过Jquery接收焦点时打开下拉菜单_Javascript_Jquery_Twitter Bootstrap_Drop Down Menu - Fatal编程技术网

Javascript 通过Jquery接收焦点时打开下拉菜单

Javascript 通过Jquery接收焦点时打开下拉菜单,javascript,jquery,twitter-bootstrap,drop-down-menu,Javascript,Jquery,Twitter Bootstrap,Drop Down Menu,所以我有一个导航栏和几个下拉菜单。长话短说,这是硬编码到我们的网站,我们需要找到一种方法来触发这些下拉没有鼠标。我尝试过其他方法,但到目前为止,我想使用以下方法: 作为一个盲人用户,我希望当他们接收到焦点时,下拉菜单打开。我相信我只需要正确的语法来触发它,但我找不到它。我正在使用这个jquery: $(document).ready(function () { $("#testF").on("focus",function() { $(this).dropdown("op

所以我有一个导航栏和几个下拉菜单。长话短说,这是硬编码到我们的网站,我们需要找到一种方法来触发这些下拉没有鼠标。我尝试过其他方法,但到目前为止,我想使用以下方法:

作为一个盲人用户,我希望当他们接收到焦点时,下拉菜单打开。我相信我只需要正确的语法来触发它,但我找不到它。我正在使用这个jquery:

$(document).ready(function () {
    $("#testF").on("focus",function() {
        $(this).dropdown("open");
    });
});
我的问题似乎在于.navbar切换(“打开”);(语法错误) 我只是想弄清楚这个下拉列表在聚焦时是如何打开的?如果您有任何帮助,我们将不胜感激。以下是一些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>

经过反复试验,我通过以下代码达到了我想要的结果(几乎,现在需要模糊,这样它就不会一直打开,欢迎使用提示!):

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

        });
    });
受此启发:

这样做的目的是在点击标签或鼠标后检测它是否是焦点

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

    });
希望这是您问题的可接受答案。

这是我的解决方案:

$(“下拉菜单按钮”)。在(“焦点”,元素=>{
var element=elements.currentTarget;
$(元素)。下拉列表(“切换”);
})

页面标题
菜单测试
菜单
https://stackoverflow.com/questions/33129942/open-a-dropdown-menu-when-it-receives-focus-via-jquery#    
您可以试试这个

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

同时发布与下拉列表相关的CSS只需使用
.navbar-toggle()
(无参数)。仍然不走运,我已经编辑了问题以包含CSSNOTE:下拉列表在鼠标悬停时打开,只是在键盘上单击时没有反应。请不要只发布代码答案。提供支持您的代码的信息。我非常喜欢这种
单击
方法!我使用此方法得到jQuery“超过最大调用堆栈大小”错误。
var lastClick = null;
    $('#testF').mousedown(function (e) {
        lastClick = e.target;
    }).focus(function (e) {
        if (e.target != lastClick) {
            $(this).dropdown("toggle");
        }
        lastClick = null;

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