簡體   English   中英

內部div為空時如何隱藏div?

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

請檢查我的jsfiddle檢查整個代碼。 如果div類timeimg為空或不存在帶有status-1或status-2類的ledBar,我試圖隱藏整個contentMachine div。

我現在有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();
})

如果ledbar類內沒有status-2或status-1類,它現在正在隱藏...我只想添加一個邏輯,當span類timeimg為EMPTY時也將其隱藏

find返回一個jQuery Object

改變-

if (display) {...}

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

參見-https://jsfiddle.net/hexa5072/1/

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

要確定某個范圍內是否沒有文本,請使用

.text() === ""

要么

.text() !== ""

取決於您是否想知道它是否為空( ==="" )或不為空( !=="" )。

更新的代碼:

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

上面說:“顯示是否有.status-1 / 2或文本不為空”。 相反,這與“如果不是.status-1 / 2且文本為空,則隱藏”相同。

如果這不符合您的確切要求,則可以更改|| &&===以及!==來獲取所需的確切邏輯。 目前尚不清楚“好”是否意味着兩者之一,或者是否必須兩者兼而有之。

您的邏輯與措辭相反(如果不是,請顯示,如果不是,則隱藏)。 使邏輯與您的措辭匹配更容易,例如:

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

它說:“隱藏是否不是status1 / 2且文本為空”

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

上面寫着:“隱藏是否不是status1 / 2或文本為空”

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM