如果span为空,则隐藏div

时间:2016-11-30 20:38:59

标签: jquery html css

如果prop-value为空,我需要隐藏 First class 。有关如何做到这一点的任何想法?如果Prop-value为空,基本上隐藏整个div

<div>
  <span class='prop-name'>First:&nbsp;&nbsp;</span><span class='prop-value'>_#= EBITDA =#_</span>
</div>

3 个答案:

答案 0 :(得分:2)

这假设您有多个,但也适用于单个

$(".prop-value").each(function(){ 
    if($(this).html().trim()==="") $(this).parent().hide();
});

答案 1 :(得分:1)

定位所有空跨度,获取父div并隐藏它

$('.prop-value:empty').closest('div').hide();
div {position: relative;height: 30px;width: 200px;background: red;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div>
    <span class='prop-name'>First:&nbsp;&nbsp;</span>
    <span class='prop-value'>Not empty</span>
</div>
<div>
    <span class='prop-name'>Second:&nbsp;&nbsp;</span>
    <span class='prop-value'><!-- is empty --></span>
</div>

答案 2 :(得分:0)

尝试

if($(".class_to_check_if_empty").html()==""){
    $(".class_to_hide").hide();
}
.class_to_hide{
  width:50px;
  height:50px;
  background-color:black;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">
</script>

<div class='class_to_check_if_empty'></div>
<div class='class_to_hide'></div>