首页 > 解决方案 > 当子 div 具有特定的类名时,隐藏 div 的类

问题描述

我通常尝试做的是,检测我们来自 Trustpilot Widget 的产品是否有 0 条评论,如果是,则显示来自 Trustpilot 的另一个小部件,它显示我们服务的评论数量。

现在,当在页面上找到一个名为“tp-stars--0”的 div 类时,我一直试图隐藏小部件。

这是我到目前为止得到的

        $('.trustpilot-widget' ).ready(function() {
          if($('.tp-widget-stars').find('.tp-stars.tp-stars--0').length) {
            $('#tp-widget-wrapper').removeClass('visible');
          }
        });
  <div id="tp-widget-wrapper" class="tp-widget-wrapper visible">
      <div id="wrapper-company-stars" class="wrapper-company-stars">
        <!-- Stars -->
        <div id="tp-widget-stars" class="tp-widget-stars">
           <div class="">
             <div class="tp-stars tp-stars--0">
               <div style="position: relative; height: 0; width: 100%; 
                 padding: 0; padding-bottom: 18.326693227091635%;">
               </div>
            </div>
         </div>
      </div>
        <div class="tp-widget-readmore" id="numberOfReviews"><strong>No 
         reviews</strong>
        </div>
      </div>
  </div>

标签: jqueryshopifytrustpilot

解决方案


尝试each在元素上使用循环并使用closest来获取父级。

    $('.tp-widget-stars').find('.tp-stars.tp-stars--0').each(function() {
       if ($(this).length) {
         $(this).closest('#tp-widget-wrapper').removeClass('visible');
       }
    });

推荐阅读