dqzg62440 2016-08-18 11:30
浏览 56
已采纳

在php foreach循环中使用jquery进行简单的折叠/展开

having a bit of trouble with a simple collapse/expand as the elements I'm try to do this with are within a foreach loop, and any element I create it's created for each list item, therefore, giving it a container is proving difficult.

PHP:

echo '<div class="cat_holder">';
function listFolderFiles($dir){
    $ffs = scandir($dir);


    foreach($ffs as $ff){

        if($ff != '.' && $ff != '..'){

            if(is_dir($dir.'/'.$ff)){

echo '<div data-toggle="collapse" class="cat_header_holder collapse in">';
echo '<h1 class="lead">' .$ff. '</h1>';
echo '</div>';

            listFolderFiles($dir.'/'.$ff);    

            }else{


?><div class="cv_group"><?php

 echo '<a target="_blank" class="cv_item" href="'.$dir.'/'.$ff.'">'.$ff.'<span class="pull-right add_shortlist"><i class="fa fa-check" aria-hidden="true"></i></span></a>';


?></div><?php

   }    
  }
 }
}

listFolderFiles('pdf');
 echo '</div>';
?>

JS:

   $('.cat_header_holder').click(function(){
    $('.cv_group').slideToggle('slow');
});

Generated HTML:

<div data-toggle="collapse" class="cat_header_holder collapse in"><h1 class="lead">Title</h1>
</div>
<div class="cv_group">
<a target="_blank" class="cv_item" href="#doc">click here</a>
</div>

<div class="cv_group">
<a target="_blank" class="cv_item" href="#doc">click here</a>
</div>

cv_group is supposed to be hidden and onclick cat_header_holder, show cv_group.

The issue is that when clicking on cat_header_holder all cv_group's are displayed and not the one for that category.

Any help is much appreciated.

  • 写回答

1条回答 默认 最新

  • doulu1020 2016-08-18 11:37
    关注

    Assuming that your final HTML will be somewhat like below you can check this code:

    $(document).ready(function() {
    
      $('.cat_header_holder').click(function() {
        $(this).nextUntil('.cat_header_holder').slideToggle('slow');
      });
    
    });
    .cv_group {
      display: none;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    <div data-toggle="collapse" class="cat_header_holder collapse in">
      <h1 class="lead">Some Text 1</h1>
    </div>
    <div class="cv_group">
      Some Contents 1 - 1
    </div>
    <div class="cv_group">
      Some Contents 1 - 2
    </div>
    <div data-toggle="collapse" class="cat_header_holder collapse in">
      <h1 class="lead">Some Text 2</h1>
    </div>
    <div class="cv_group">
      Some Contents 2 - 1
    </div>
    <div class="cv_group">
      Some Contents 2 - 2
    </div>
    <div class="cv_group">
      Some Contents 2 - 3
    </div>
    <div data-toggle="collapse" class="cat_header_holder collapse in">
      <h1 class="lead">Some Text 3</h1>
    </div>
    <div class="cv_group">
      Some Contents 3
    </div>

    Here via $.nextUntil() we are targeting the next DOM elements i.e. associated div which need to be toggled.

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度