dragon_9000 2018-03-19 13:00
浏览 83
已采纳

jQuery - 检查div是否有内容,如果没有将类添加到顶级div

I'm really hoping someone can help me. I am having issues with this page: http://dd-server.co.uk/vinetrail/producers/

Each region has a number of producers which are shown via collapsible divs. It's all working fine, but when you use the filter at the top of the page, it still works fine except the empty regions are still shown.

I want to use jQuery to detect the empty divs and hide the whole region, so only regions with producers are shown. Here is my code:

<div id="producer-list">            
            <?php
                $region_group_terms = get_terms( 'producer_region' );
                $c = 0;
                $id = 'section1';

                foreach ( $region_group_terms as $region_group_term ) {
                    $c++;
                    $producers_query = new WP_Query( array(
                        'post_type' => 'producers',                         
                        'orderby'   => 'title',
                        'order'     => 'ASC',
                        'tax_query' => array(
                            array(
                                'taxonomy' => 'producer_region',
                                'field' => 'slug',
                                'terms' => array( $region_group_term->slug ),
                                'operator' => 'IN'
                            )
                        ),
                        'search_filter_id'  => 788
                    ) );
                ?>

                <div class="pro-wrap">                  
                    <?php if($c == 1) : ?>
                        <div class="collapsible collapse-open" id="<?php echo $id; ?>"><h2><?php echo $region_group_term->name; ?><span></span></h2></div>
                    <?php else : ?>
                        <div class="collapsible" id="<?php echo $region_group_term->name; ?>"><h2><?php echo $region_group_term->name; ?><span></span></h2></div>
                    <?php endif; ?>

                        <div class="container">
                            <div class="content">
                                <?php
                                    if ( $producers_query->have_posts() ) : while ( $producers_query->have_posts() ) : $producers_query->the_post(); ?>
                                        <div class="one-quarter">                                           
                                            <?php if ( has_post_thumbnail() ) : ?>
                                                <a href="<?php echo get_permalink(); ?>"><?php echo get_the_post_thumbnail(); ?></a>
                                            <?php else : ?>
                                                <a href="<?php echo get_permalink(); ?>"><img src="<?php echo get_template_directory_uri(); ?>/img/producer-default.jpg" alt="<?php echo the_title(); ?>" /></a>
                                            <?php endif; ?>

                                            <h3>
                                                <a href="<?php echo the_permalink(); ?>">
                                                    <?php echo $region_group_term->name; ?>
                                                    <span><?php echo the_title(); ?></span>
                                                </a>
                                            </h3>                                           
                                        </div>
                                    <?php endwhile; endif; ?>

                                <?php
                                    $member_group_query = null;
                                    wp_reset_postdata();
                                ?>
                            </div>
                        </div>
                    </div>
                    <?php } ?>
        </div>          
  • 写回答

1条回答 默认 最新

  • dpgvdfg321041670 2018-03-19 13:21
    关注

    You can try this, in jQuery

    Try 1

    jQuery( document ).ready(function() {
      // Loop over hidden element with class container 
      jQuery.each(jQuery('.pro-wrap .container:hidden'), function(i, ele){
        // Hide closest parent found with class pro-wrap
        jQuery(ele).closest('.pro-wrap').hide();
      });
    });
    

    Try 2

    jQuery( document ).ready(function() {
      // Loop over element with class content 
      jQuery.each(jQuery('.pro-wrap .container .content'), function(i, ele){
        var jEle = jQuery(ele);
        // If empty content
        if (jEle.text().trim().length == 0) {
          // Hide closest parent found with class pro-wrap
          jEle.closest('.pro-wrap').hide();
        }
      });
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗