简体   繁体   English

如果 ul 为空,则隐藏 div

[英]Hide div if the ul is empty

Here is my html ,这是我的html

<div id="personaldetails">

    <ul>
        <li class="clear"></li> 
    </ul>
    <ul>
        <li class="clear"></li> 
    </ul>
</div>

I want to hide div personaldetails when all the ul inside in div is empty.div所有 ul 为空时,我想隐藏div personaldetails

If the ul is having element <li class="clear"></li> then the ul is considered as to be empty.如果ul有元素<li class="clear"></li>ul被认为是空的。

How to do this using Jquery ?如何使用 Jquery 做到这一点?

You can try this:你可以试试这个:

$('#personaldetails').find('ul').each(function(){
   var txt = $("li", this).text();
   if(txt.length <= 0){
      $(this).hide();
   }
});
if(!$('#personaldetails').find('ul:visible').length){
    $('#personaldetails').hide();
}

Updated Fiddle更新的小提琴


And to me you should hide all ul , if no ul are visible then you can hide the #personaldetails div.对我来说,你应该隐藏所有ul ,如果没有 ul 可见,那么你可以隐藏#personaldetails div。

Even one of answer is already accepted, I think it can be simple as:甚至一个答案已经被接受,我认为它可以很简单:

if($.trim($("#personaldetails").text()) == '') {
    $("#personaldetails").hide();
}

:) :)

Take a look at that code:看看那个代码:

function foo(){
    var all_li_clear = true;
    $("#personaldetails > ul > li").each(function(){
        if(!$(this).hasClass("clear")){
            all_li_clear = false;
            break; // No need to continue now
        }
    });

    if(all_li_clear){
        $("#personaldetails").hide();
    }
}

You can see a fiddle example there , just comment and discomment foo();您可以在那里看到一个小提琴示例,只需注释和取消注释foo(); line.线。

Javascript solution: Javascript 解决方案:

This will only hide the div if all li have clear class如果所有 li 都有明确的类,这只会隐藏 div

        $(function() {
         emptyLi = $('#personaldetails ul li').filter(function(){

            /*if($(this).hasClass('clear')){
             return true;
            }else{
             return false;
            }*/
            return $(this).hasClass('clear');

         }).length;

         if($('#personaldetails ul li').length == emptyLi){

           $('#personaldetails').css('display','none');
         }
        });

CSS: CSS:

This will hide the li with class clear, so if you not fixed height of ul or li and don't have padding , margin given to ul,li your div personaldetails will get hidden automatically when all li element have class clear这将隐藏类 clear 的 li,所以如果你没有固定 ul 或 li 的高度并且没有填充,给 ul,li 的边距,当所有 li 元素都有类 clear 时,你的 div 个人详细信息将自动隐藏

        #personaldetails ul li.clear{
          display:none;
        }

- UPDATED - -更新-

You can use following code if you are deciding empty class based on clear class.如果您根据clear class 决定空 class,则可以使用以下代码。

if($("#personaldetails ul li:not(.clear)").length == 0) {
    $("#personaldetails").hide();
}

Or if you are looking for the empty div then you can just use the shortest code given by @Samiul Amin Shanto Like :或者,如果您正在寻找空的 div,那么您可以使用@Samiul Amin Shanto Like给出的最短代码:

if($.trim($("#personaldetails").text()) == '') {
    $("#personaldetails").hide();
}

Explanations说明

Method1 :方法一

$("#personaldetails ul li:not(.clear)") This code find all li without the clear class. $("#personaldetails ul li:not(.clear)")此代码查找所有没有clear类的li Then if no such li found, just hide the div .然后如果没有找到这样的li ,只需隐藏div Fiddle小提琴

Method2 :方法二

$("#personaldetails").text() this code return innerHTML text striping all html tags. $("#personaldetails").text()此代码返回对所有 html 标签进行条带化处理的 innerHTML 文本。 So no meter what the div contain ul, li or anything else, this will return the plain text content of the div, then striping any white space we can determine if the div is empty.因此,没有计量 div 包含 ul、li 或其他任何内容,这将返回 div 的纯文本内容,然后去除任何空白,我们可以确定 div 是否为空。 If your intention is to hide the empty div not hiding the div which contain empty Ul this should be your choice.如果您的意图是隐藏空 div 而不是隐藏包含空Ul的 div,这应该是您的选择。

$(function($) {
    $cnt = 0;
    $('.personalDetails ul li').each(function() {
        if($(this).hasClass('clear')) $cnt++;
    });
    if($('.personalDetails ul li').length == $cnt) $('.personalDetails').hide();
});

This asumes that if you have the same amount of li's with the class clear, as there are ul's, they're all empty这假设如果你有相同数量的 li 和 class clear,因为有 ul,它们都是空的

var $wrapper = $('#personaldetails');
if(  $wrapper.find('ul').length=== $wrapper.find('li.clear').length){ 
    $wrapper .hide(); 
}

Everybody's fiddling examples :)每个人都在摆弄例子:)

$("ul li:empty").closest('div#personaldetails').hide();

示例代码

#personaldetails ul li.clear{
    visibility:hidden;
}

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

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