且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 Bootstrap 3 中单击导航栏元素外部时如何关闭打开的折叠导航栏?

更新时间:2023-01-06 18:24:35

看看:

$(document).ready(function () {
    $(document).click(function (event) {
        var clickover = $(event.target);
        var _opened = $(".navbar-collapse").hasClass("navbar-collapse in");
        if (_opened === true && !clickover.hasClass("navbar-toggle")) {
            $("button.navbar-toggle").click();
        }
    });
});

您的小提琴适用于:http://jsfiddle.net/52VtD/5718/

它是 这个答案,缺少动画,也稍微复杂一点.

Its a modified version of this answer, which lacks the animation and is also a tiny bit more complicated.

我知道,调用 click() 不是很优雅,但是 collapse('hide') 对我也不起作用,我认为动画比几乎添加和删除类要好一些.

I know, invoking the click() isn't very elegant, but collapse('hide') did not work for me either, and i think the animation is a bit nicer than adding and removing the classes hardly.