簡體   English   中英

當另一個div為空時隱藏div

[英]Hide a div when another div is empty

我知道這個問題已經在其他主題中提出過,但是由於某種原因,我無法在WordPress網站上使用此代碼。

 if ($(".objectA").html().length == 0) { $("#objectB").hide(); } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="objectA"></div> <div id="objectB">hide this div</div> 

我真的不明白為什么#objectB沒有隱藏在我的頁面上。 當我使用開發人員工具檢查代碼時,我可以看到它在那里。

嘗試更換:

if ($(".objectA").html().length == 0) {
    $("#objectB").hide();
}

附:

if (!$(".objectA").html()) {
    $("#objectB").hide();
}

嘗試添加文檔准備功能。 無需單擊,頁面加載后即可運行文檔准備工作。

$( document ).ready(function() {
   if ($(".objectA").html().length == 0) {
       $("#objectB").hide();
   }
});

進一步搜索之后,我自己找到了解決方案。

WordPress不僅僅接受if($ ...,它需要在函數前使用jQuery(function ($) {

jQuery(function ($) {
    if($(".empty-div").html().length ==0)
    {
    $("#deals-wrapper, .listing-details li.vc_tta-tab:nth-child(n+3):nth-child(-n+3)").hide();
    }
});

暫無
暫無

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

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