简体   繁体   English

当另一个div为空时如何隐藏div

[英]How to hide a div when another div is empty

I'm new to jQuery and I've hit a bit of a snag. 我是jQuery的新手,我遇到了一些麻烦。

Here is my HTML: 这是我的HTML:

<div id="mainpage-tabs-area">
<ul class="nav cf">
    <li class="tb-one"><a href="#description" class="current">BTN1</a></li>
    <li class="tb-two"><a href="#tabtwo">BTN2</a></li>
    <li class="tb-three"><a href="#tabthree">BTN3</a></li>
    <li class="tb-four"><a href="#tabfour">BTN4</a></li>
</ul>

<div class="list-wrap">
    <ul id="description">
        <li>Hello, I am a description</li>
    </ul>

    <ul id="tabtwo" class="hide">
      <li></li>
    </ul>

    <ul id="tabthree" class="hide">
        <li></li>
    </ul>

    <ul id="tabfour" class="hide">
        <li></li>
    </ul>
</div> <!-- END List Wrap -->
            </div> <!-- END Organic Tabs -->

And here is my current jQuery: 这是我目前的jQuery:

$(document).ready(function () {
    var tabContent = $('ul.list-wrap').find('li').text();
    if ($.trim(tabContent) === "") {
        $('ul.nav li').hide();
    }
});

What I want to be able to do is just the jQuery to check every li in .list-wrap and if that li is empty then remove the corresponding li in .nav as this is for a tab system where if no content is entered, they don't want it displaying. 我想要做的只是jQuery检查.list-wrap中的每个li,如果li是空的,那么删除.nav中的相应li,因为这是一个制表系统,如果没有输入内容,他们不希望它显示。

Any suggestions for a newbie? 对新手的任何建议?

Nick 缺口

I'd iterate through the list items, check the length of each node's text, and hide the corresponding tab with: 我将遍历列表项,检查每个节点文本的长度,并隐藏相应的选项卡:

 $('.list-wrap li').each(function(i){ if(!$(this).text().length) $('#mainpage-tabs-area li').eq(i).hide(); }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div id="mainpage-tabs-area"> <ul class="nav cf"> <li class="tb-one"><a href="#description" class="current">BTN1</a> </li> <li class="tb-two"><a href="#tabtwo">BTN2</a> </li> <li class="tb-three"><a href="#tabthree">BTN3</a> </li> <li class="tb-four"><a href="#tabfour">BTN4</a> </li> </ul> <div class="list-wrap"> <ul id="description"> <li>Hello, I am a description</li> </ul> <ul id="tabtwo" class="hide"> <li></li> </ul> <ul id="tabthree" class="hide"> <li></li> </ul> <ul id="tabfour" class="hide"> <li></li> </ul> </div> <!-- END List Wrap --> </div> <!-- END Organic Tabs --> 

The i in the each() function is the index of the element that the loop is on, and that allows you to target the corresponding tab element and hide it. each()函数中的i是循环所在元素的索引,它允许您定位相应的制表符元素并将其隐藏。

This should do the job: 这应该做的工作:

$('.list-wrap ul li').each(function(){
    var cnt = $('.list-wrap ul li').index( $(this) );
    var txt = $(this).text();
    if ($.trim(txt)==''){
        $('.nav li').eq(cnt).hide();
    }
});

jsFiddle Demo jsFiddle演示

I would suggest this If you are not making infinite lists 我建议如果你没有制作无限列表

 $('.tb-two').hide(); //if tabtwo is empty $('.tb-hree').hide(); //if tabthree is empty and so on... 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM