jQuery函数不隐藏部分

时间:2016-08-02 03:07:23

标签: jquery

我使用一个函数隐藏空容器,但它不适用于一个部分。

以下是代码:

$( '.lmla-hidden' ).each(function() {
      if ( '' === $.trim( $( this ).text() ) ) {
        $( this ).hide();
      }
    });

这是应用它的地方:

<section class="section-wrapper-projects lmla-hidden">

          <h2 class="section-title lmla-hidden"><?php echo CFS()->get('course_details_title_project'); ?></h2>

          <div class="projects-description lmla-hidden">
                        <?php echo CFS()->get('design_cycle'); ?>
                    </div>

          <div class="single-project-wrapper lmla-hidden">
                        <p class="project-title lmla-hidden"><?php echo CFS()->get('project_title'); ?></p>

                        <div class="project-image lmla-hidden"><img src="<?php echo CFS()->get('project_image'); ?>"></div>

                        <style> .project-link-btn a {color: <?php echo CFS()->get('link_text_colour'); ?>;} </style>
                        <style> .project-link-btn {background: <?php echo CFS()->get('project_link_colour'); ?>;} </style>

                        <div class="project-link-btn lmla-hidden"><?php echo CFS()->get('projects_link'); ?></div>

                    </div>

      </section>

padding-bottom属性仍然应用于该部分。

1 个答案:

答案 0 :(得分:1)

似乎一切正常。

&#13;
&#13;
$('.lmla-hidden').each(function() {
  if (!$.trim($(this).text())) {
    $(this).hide();
  }
});
&#13;
.project-link-btn a {
  color: red;
  background: black;
}

.project-link-btn {
  background: black;
}

.lmla-hidden {
  border: 1px solid black;
  margin: 5px;
  padding: 3px;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<section class="section-wrapper-projects lmla-hidden">

  <h2 class="section-title lmla-hidden">Test</h2>

  <div class="projects-description lmla-hidden">
    Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?
  </div>
  
  <div class="projects-description lmla-hidden">
    <!-- An empty element will be hidden -->
  </div>

  <div class="single-project-wrapper lmla-hidden">
    <p class="project-title lmla-hidden">
      Test
    </p>

    <div class="project-image lmla-hidden"><img src="some-img.jpg"></div>

    <div class="project-link-btn lmla-hidden">
      <a href="#">qwerty</a>
    </div>

  </div>

</section>
&#13;
&#13;
&#13;