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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器