简体   繁体   English

如果另一个div为空,则将div隐藏在列表项中

[英]Hide div inside list item if another div is empty

I have several <div> elements of different classes within each <li> element. 我在每个<li>元素中有几个不同类的<div>元素。 If one of them having a testsimonial-content class is empty, how can I hide another div of class testsimonial-heading within the same <li> as well? 如果其中一个具有testsimonial-content类别的testsimonial-content空,那么如何在同一个<li>中也隐藏class testsimonial-heading另一个div It shouldn't affect other <li> s. 它不应该影响其他<li>

Here is my HTML code: 这是我的HTML代码:

<ul>
    <li>
        <div class="testsimonial-heading">Testimonial</div>
        <div class="testsimonial-content">Hello this is a testimonial</div>
    </li>
    <li>
        <div class="testsimonial-heading">Testimonial</div> <!--This div should be hidden-->
        <div class="testsimonial-content"></div>
    </li>
    <li>
        <div class="testsimonial-heading">Testimonial</div>
        <div class="testsimonial-content">Hello this is a testimonial</div>
    </li>
</ul>

 $('#ul li').each(function() { var div = $(this).find('.testsimonial-content'); console.log(div.text().length); if (div.text().length === 0) { div.siblings('.testsimonial-heading').hide(); } }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <ul id='ul'> <li> <div class="testsimonial-heading">Testimonial</div> <div class="testsimonial-content">Hello this is a testimonial</div> </li> <li> <div class="testsimonial-heading">Testimonial1232131</div> <!--This div should be hidden--> <div class="testsimonial-content"></div> <div class="testsimonial-">Other div</div> </li> <li> <div class="testsimonial-heading">Testimonial</div> <div class="testsimonial-content">Hello this is a testimonial</div> </li> </ul> 

Find the class testsimonial-content using .find() get its length using .length and if zero hide its sibling using .siblings().hide() 使用.find()查找类testsimonial-content使用.length获得其长度,如果为零,则使用.siblings().hide()隐藏其同级.siblings().hide()

It is as simple as this: 就这么简单:

$(document).ready(function() {
    $(".testsimonial-content:empty").parent().hide(); 
    // if content is empty - then hide
});

or this one if you need to hide li if any of div s is empty 如果您需要隐藏li或者如果div任何一个为空,则为this

$(document).ready(function() {
    $(".testsimonial-content:empty, .testsimonial-heading:empty").parent().hide(); 
    // if content OR heading is empty - then hide
});

Here is the working JSFiddle demo . 这是工作中的JSFiddle演示

Update from the OP: 从OP更新:

Sorry I actually have more than 2 divs within each li, can you make it hide the class .testimonial-heading? 抱歉,我实际上在每个li内有2个以上的div,您可以使其隐藏类.testimonial-heading吗? What would be the updated javascript for this? 什么是为此更新的javascript? Instead of hiding parent. 而不是隐藏父级。

Here you go: 干得好:

$(document).ready(function() {
    $(".testsimonial-content:empty").siblings(".testsimonial-heading").hide(); 
    // if content is empty - then hide heading in this li only
});

Updated JSFiddle . 更新了JSFiddle

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

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