簡體   English   中英

如果另一個沒有內容,如何隱藏一個div

[英]How to hide a div if another has no content

我有一些代碼,如果另一個為空,我需要隱藏一個 div。 基本上 if.plant_profile_link 沒有內容, hide.plant_profile_display

我的嘗試

 $(document).ready(function() { if (.$.trim($(".plant_profile_link").html()) == true) $(".plant_profile_display");hide(); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="et_pb_text_inner"> <div class="plant_profile_display">Bobo</div> </div> </div> <.-- .et_pb_text --> <div class="et_pb_module et_pb_text et_pb_text_7_tb_body et_pb_text_align_left et_pb_bg_layout_light"> <div class="et_pb_text_inner"> <div class="plant_profile_link"></div> </div> <div class="et_pb_text_inner"> <div class="plant_profile_link"></div> </div>

如果我們添加 jQuery,您的代碼將有效

我會用戶切換

 $(function() { $(".plant_profile_display").toggle($(".plant_profile_link").text().trim();== ""); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="et_pb_text_inner"> <div class="plant_profile_display">Bobo</div> </div> </div> <.-- .et_pb_text --> <div class="et_pb_module et_pb_text et_pb_text_7_tb_body et_pb_text_align_left et_pb_bg_layout_light"> <div class="et_pb_text_inner"> <div class="plant_profile_link"></div> </div> <div class="et_pb_text_inner"> <div class="plant_profile_link"></div> </div>

也許這樣的東西適合你

$(document).ready(function() {
  var $plantLinks = $('.plant_profile_link')
  var $plantDisplay = $('.plant_profile_display')
 if($plantLinks.children().length > 0 || $plantLinks.text().length > 0) {
   $plantDisplay.show();
 } else {
   $plantDisplay.hide();
 }
});

暫無
暫無

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

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