根据条件隐藏Div

时间:2013-06-24 15:16:49

标签: jquery html

您好我有以下HTML结构作为示例:

<body>


Testing hiding DIVs

<div class="content-1">
<h2>Hello - this is div 1</h2>


<div class="content-2">
<h2>Hello - this os div 2</h2>


</body>

我想隐藏div“content-2”,只有div“content-1”包含或包含内容。

使用jquery实现此目的的最简单方法是什么?

非常感谢任何建议......

3 个答案:

答案 0 :(得分:5)

你可以试试这个 - (你应该关闭你的div但是

if($('.content-1').contents().length > 0){
  $('.content-2').hide();
}

答案 1 :(得分:1)

if ($('div.content-1:not(:empty)').length) $('div.content-2').hide();

答案 2 :(得分:-1)

if($(".content-2").html().length > 0)
    $(".content-1").hide();

编辑 - 误读了这个最初改变的布尔表达式。