如何检查Jquery是否有任何可见的div?

时间:2017-01-17 07:10:41

标签: javascript jquery

我有一个HTML文档,我想在页面加载后检查是否有任何可见的div元素和Jquery。

在文档就绪函数之后,一些js函数将生成多个div元素,我想检查生成与否的这些div元素。

我找到了下面的代码,但它检查了ID,但我没有任何ID;

func back(sender: UIBarButtonItem) {

   // perform your custom action 
   //....
   // go back to the previuos view controller 
   _ = navigationController?.navigationController?.self.dismiss(animated: true)

 }

3 个答案:

答案 0 :(得分:4)

  

我想检查是否有任何可见的div元素与JQuery

if ($("div:visible").length)

如果至少看到1个div,则返回true

答案 1 :(得分:0)

为什么不尝试搜索div而不是div Id:

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" type="text/css">



  <div class="container">
<nav class="navbar navbar-default">
  <div class="container">
    <!-- Brand and toggle get grouped for better mobile display -->
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand" href="#">Bootstrap nav menu</a>
    </div>

    <!-- Collect the nav links, forms, and other content for toggling -->
    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
      <ul class="nav navbar-nav">
       
        <li class="dropdown">
          <a href="https://www.google.com" class="dropdown-toggle disabled" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Main menu 1<span class="caret"></span></a>
          <ul class="dropdown-menu">
            <li><a href="#">submenu1</a></li>
            <li><a href="#">submenu2</a></li>
            <li><a href="#">submenu3</a></li>
            
          </ul>
        </li>
         <li class="active"><a href="#">Main menu 2 <span class="sr-only">(current)</span></a></li>
        <li><a href="#">Main menu 3</a></li>
      </ul>     
    </div><!-- /.navbar-collapse -->
  </div><!-- /.container-fluid -->
</nav>
  </div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js">

答案 2 :(得分:0)

如果有任何div,我认为这足够了:

if($('div')){console.log('present');}