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

时间:2015-10-23 03:53:07

标签: javascript jquery hide

我在每个<div>元素中有几个不同类的<li>个元素。如果其中一个testsimonial-content类是空的,我如何在同一div内隐藏另一个testsimonial-heading<li>?它不应该影响其他<li>

这是我的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>

2 个答案:

答案 0 :(得分:1)

$('#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>

使用testsimonial-content使用.find()查找课程.length,使用.siblings().hide()获取其长度,如果为零则使用onvalidation隐藏其兄弟

答案 1 :(得分:1)

就这么简单:

li
如果任何 div s为空,则需要隐藏$(document).ready(function() { $(".testsimonial-content:empty, .testsimonial-heading:empty").parent().hide(); // if content OR heading is empty - then hide });

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

这是working JSFiddle demo

从OP更新:

  

对不起,我实际上每个li里面有两个以上的div,你可以让它隐藏这个类.testimonial-heading?什么是更新的JavaScript这个?而不是隐藏父母。

你走了:

UPDATE a SET a.c2 = sum(b.c2)
FROM T1 a INNER JOIN t2 b ON a.c1 = b.c1     
GROUP BY b.c1

更新了JSFiddle