【问题标题】:Hiding class of a div, when a child div has the specific class name当子 div 具有特定的类名时,隐藏 div 的类
【发布时间】:2019-10-11 06:26:46
【问题描述】:

我通常尝试做的是,检测来自 Trustpilot 小部件的产品是否有 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>

【问题讨论】:

    标签: jquery shopify trustpilot


    【解决方案1】:

    从元素开始,找到父元素。

    $('.tp-stars--0').parent('#tp-widget-wrapper').removeClass('visible');
    

    【讨论】:

    • .parent(),不是吗?
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2014-09-25
      • 1970-01-01
      • 2021-12-14
      • 1970-01-01
      • 2021-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-20
      相关资源
      最近更新 更多