简体   繁体   English

内部div为空时如何隐藏div?

[英]How do I hide a div when an inside div is empty?

Please check my jsfiddle to check the whole code. 请检查我的jsfiddle检查整个代码。 I am trying to hide the whole contentMachine div if the div class timeimg is empty or no ledBar with status-1 or status-2 classes are present. 如果div类timeimg为空或不存在带有status-1或status-2类的ledBar,我试图隐藏整个contentMachine div。

I have the ff code right now: 我现在有ff代码:

$(function() {
    function showHideEmptyBlocks() {
        $(".contentMachine").each(function() {
            // var isVisible=$(this).find(".timeimg").text() && $(this).find(".timeimg").text().length;
            var display=$(this).find(".ledBar").is('.status-1, .status-2');
            //console.log('display >' , display);
            if(display) {
                $(this).show();
            } else {
                $(this).hide();
            }
        })

        setTimeout(function(){
            showHideEmptyBlocks();
        },1);
    }
    showHideEmptyBlocks();
})

It is now hiding if there is no status-2 or status-1 class inside the ledbar class... I just wanna add a logic that hides it as well when span class timeimg is EMPTY 如果ledbar类内没有status-2或status-1类,它现在正在隐藏...我只想添加一个逻辑,当span类timeimg为EMPTY时也将其隐藏

find returns an jQuery Object . find返回一个jQuery Object

change - 改变-

if (display) {...}

to

if (display.length > 0) {...}

See - https://jsfiddle.net/hexa5072/1/ 参见-https://jsfiddle.net/hexa5072/1/

Working Fiddle - https://jsfiddle.net/pLkqh0d7/ 工作小提琴-https: //jsfiddle.net/pLkqh0d7/

To determine if a span has no text inside, use 要确定某个范围内是否没有文本,请使用

.text() === ""

or 要么

.text() !== ""

depending on whether you want to know if it's empty ( ==="" ) or not empty ( !=="" ). 取决于您是否想知道它是否为空( ==="" )或不为空( !=="" )。

Updated code: 更新的代码:

var display = $(this).find(".ledBar").is('.status-1, .status-2')
                || $(this).find(".timeimg").text() !== "";
$(this).toggle(display);

The above says: "show if there is a .status-1/2 or the text is not empty". 上面说:“显示是否有.status-1 / 2或文本不为空”。 Conversely, this is the same as "hide if it's not .status-1/2 and the text is empty". 相反,这与“如果不是.status-1 / 2且文本为空,则隐藏”相同。

If this doesn't match your exact requirement, you can change || 如果这不符合您的确切要求,则可以更改|| with && and === with !== to get the exact logic you need. &&===以及!==来获取所需的确切逻辑。 As it's not clear if "as well" means either or if it must be both. 目前尚不清楚“好”是否意味着两者之一,或者是否必须两者兼而有之。

Your logic is the opposite of your wording (show if not this, vs hide if this). 您的逻辑与措辞相反(如果不是,请显示,如果不是,则隐藏)。 It can be easier to make the logic match what your wording, eg: 使逻辑与您的措辞匹配更容易,例如:

var hide = !$(this).find(".ledBar").is('.status-1, .status-2')
                && $(this).find(".timeimg").text() === "";
$(this).toggle(!hide);

which says: "hide if it's not status1/2 and the text is empty" 它说:“隐藏是否不是status1 / 2且文本为空”

var hide = !$(this).find(".ledBar").is('.status-1, .status-2')
                || $(this).find(".timeimg").text() === "";
$(this).toggle(!hide);

which says: "hide if it's not status1/2 or the text is empty" 上面写着:“隐藏是否不是status1 / 2或文本为空”

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

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