Bootstrap 3 在页面加载时使用 Javascript 打开下拉菜单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21563535/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 21:11:57  来源:igfitidea点击:

Bootstrap 3 Open Drop Down on page load with Javascript

javascriptjquerydrop-down-menutwitter-bootstrap-3

提问by duyn9uyen

I can't seem to open this dropdown menu on page load. Can anyone help? Documentation on Bootrap 3 is here: http://getbootstrap.com/javascript/#dropdowns

我似乎无法在页面加载时打开此下拉菜单。任何人都可以帮忙吗?Bootrap 3 的文档在这里:http://getbootstrap.com/javascript/#dropdowns

I tried this but it doesn't seem to work

我试过这个,但它似乎不起作用

$('#myDropdown').dropdown()

Ultimately, I need to be able to open a drop down that is inside a collapsible menu. For example, I want to open the first down down menu after the user clicks on the menu button. but I can't get it to work.

最终,我需要能够打开可折叠菜单内的下拉菜单。例如,我想在用户单击菜单按钮后打开第一个向下菜单。但我无法让它工作。

http://jsfiddle.net/G4k4F/3

http://jsfiddle.net/G4k4F/3

Edit: When the navbar is shown, use a timer to wait 1 millisecond before calling .dropdown('toggle'). Like this.

编辑:显示导航栏时,使用计时器等待 1 毫秒,然后再调用 .dropdown('toggle')。像这样。

function OpenDropDown() {
    $('.dropdown-menu').dropdown('toggle');
};

$('.collapse.navbar-collapse').on('show.bs.collapse', function () {
    window.setTimeout(OpenDropDown, 1);
});

回答by Jér?me Teisseire

try this :

试试这个 :

$(function () {
  $('.dropdown-menu').dropdown('toggle');
});

回答by Zim

Try this..

试试这个..

$(function () {
    $('[data-toggle="dropdown"]').dropdown('toggle');
});

Or you can use the button id like:

或者您可以使用按钮 id,如:

 $('#dLabel').dropdown('toggle');