twitter-bootstrap 如何找出“导航栏切换”按钮是否已使用 Javascript 折叠或隐藏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37995824/
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-22 00:03:04  来源:igfitidea点击:

How to find out if "navbar-toggle" button is collapsed or hidden with Javascript

javascripttwitter-bootstrapbutton

提问by Turpal Iliev

Is there a way to find out, with JS, if the toggle button menu is collapsed or hidden.

有没有办法用 JS 找出切换按钮菜单是否折叠或隐藏。

Does class="navbar-toggle"or data-toggle="collapse"have a parameter which indicates this state?

难道类=“导航栏拨动”或数据切换=“崩溃”有这表示在此状态下的参数?

I know the events that are called when the ".collape" is shown or hidden:

我知道显示或隐藏“.collape”时调用的事件:

$('.navbar-collapse').on('shown.bs.collapse', function () { //called when dropdown menu is shown });

$('.navbar-collapse').on('shown.bs.collapse', function () { //called when dropdown menu is shown });

Furthermore, I know the methods that shows or hides the collapse like this:

此外,我知道显示或隐藏折叠的方法,如下所示:

$('.navbar-collapse').collapse('hide'); 

But I can′t find any information on how to get the current status of .collapse, telling me if it′s hidden or shown.

但是我找不到任何关于如何获取 .collapse 当前状态的信息,告诉我它是隐藏还是显示。

回答by Nikki9696

(I am editing this answer with a better way.)

(我正在用更好的方式编辑这个答案。)

Yes, you can check if the element is visible, using jquery $(element).is(":visible")

是的,您可以使用 jquery $(element).is(":visible") 检查元素是否可见

Here is a live demo on jsFiddle

这是一个关于 jsFiddle现场演示

Here is some html

这是一些html

<nav class="navbar navbar-inverse">
  <div class="container-fluid">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand" href="#">WebSiteName</a>
    </div>
    <div class="collapse navbar-collapse" id="myNavbar">
      <ul class="nav navbar-nav">
        <li class="active"><a href="#">Home</a></li>
        <li class="dropdown">
          <a class="dropdown-toggle" data-toggle="dropdown" href="#">Page 1 <span class="caret"></span></a>
          <ul class="dropdown-menu">
            <li><a href="#">Page 1-1</a></li>
            <li><a href="#">Page 1-2</a></li>
            <li><a href="#">Page 1-3</a></li>
          </ul>
        </li>
        <li><a href="#">Page 2</a></li>
        <li><a href="#">Page 3</a></li>
      </ul>
      <ul class="nav navbar-nav navbar-right">
        <li><a href="#"><span class="glyphicon glyphicon-user"></span> Sign Up</a></li>
        <li><a href="#"><span class="glyphicon glyphicon-log-in"></span> Login</a></li>
      </ul>
    </div>
  </div>
</nav>

<div class="container">
  <h3>Collapsible Navbar</h3>
  <p>In this example, the navigation bar is hidden on small screens and replaced by a button in the top right corner (try to re-size this window).
  <p>Only when the button is clicked, the navigation bar will be displayed.</p>
</div>
<input type="button" value="click me" onclick="clickedIt()" />

Here is some javascript

这是一些 javascript

function clickedIt() {   
   var canSee = $("#myNavbar").is(":visible");
   alert(canSee);
}