如何使用jQuery折叠嵌套列表? [英] How can I collapse a nested list using jQuery?

查看:53
本文介绍了如何使用jQuery折叠嵌套列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个嵌套列表:

<ul>
  <li><a href='#'>stuff</a></li>
  <li><a href='#'>stuff2</a></li>
    <ul>
      <li><a href='#'>stuff3</a></li>
    </ul>
  <li><a href='#'>stuff4</a></li>
</ul>

...,并且要在单击li时折叠嵌套的ul.在我使用之前

...and want to collapse the nested ul when the li is clicked. Before I was using

$('UL LI').click(function(){
  $(this).next().slideToggle();
});

...但是当li后面没有嵌套ul时,这显然会引起问题.是否有更好的方法可以执行此操作,或者我可以确定$(this).next()返回的对象是否为UL?

...but this obviously causes a problem when a li doesn't have a ul nested after it. Is there a better way to do this, or is there a way for me to determine if the object returned by $(this).next() is a UL?

推荐答案

if($(this).next().is("ul")) {

}

应该做到这一点.请参阅: http://api.jquery.com/is/

Should do the trick. See: http://api.jquery.com/is/

这篇关于如何使用jQuery折叠嵌套列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆