drnycqxwz63508434 2016-12-07 10:13
浏览 85
已采纳

如何使用jQuery切换指定要显示的无序列表

Hello I am trying to use the jQuery toggle function just to change a ul display state from:

display: none;

to

display: block;

and this works however I am receiving the information from the database and with jQuery toggle it is showing all of the information not specifically from category id in which i click on

Here's what I have so far:

<section id="categories-wrapper">
  <div class="cat-center">
    <?php foreach($categories as $cat) { ?>
    <?php if($cat['parent_id'] < 1) { ?>
    <li>
      <a href="#!"><img src="media/categories/<?php echo $cat['image']; ?>"></a>
      <ul>
        <?php
$childCategories = $objCatalogue->getChildCategories($cat['id']);
?>
        <?php foreach($childCategories as $childCat) { ?>
        <li><?php echo $childCat['name']; ?></li>
        <?php } ?>
      </ul>
    </li>
    <?php } ?>
    <?php } ?>
  </div>
</section>

the js code:

$('.cat-center li').click(function() {
    event.stopPropagation();
    $('.cat-center li ul').toggle('blind');
});

Now with the jQuery toggle when i click on the first link it shows the information from the other link as well, but with CSS using the hover command to just change display to block it works. Could someone please advise on what I am doing wrong.

</div>
  • 写回答

1条回答 默认 最新

  • douzhang5984 2016-12-07 10:39
    关注

    Try changing your $('.cat-center li ul').toggle('blind');

    to

    $('.cat-center li').click(function() {
        event.stopPropagation();
        if ($(this).find('ul').is(":visible"))
        {
            $(this).find('ul').hide(500);
        }
        else
        {
          $('.cat-center li ul').hide(500);
          $(this).find('ul').toggle('blind');
        }
    });
    

    https://jsfiddle.net/m6jLo547/

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

报告相同问题?

悬赏问题

  • ¥50 comsol稳态求解器 找不到解,奇异矩阵有1个空方程返回的解不收敛。没有返回所有参数步长;pid控制
  • ¥15 怎么让wx群机器人发送音乐
  • ¥15 fesafe材料库问题
  • ¥35 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功