简体   繁体   English

适用于SlideToggle的jQuery父母与子女

[英]jQuery Parent & Children for SlideToggle

I have built a CSS Menu for a shopping site. 我已经为购物网站构建了CSS菜单。 The idea is the main category gets clicked then the slideToggle kicks in and loads the subcategories. 这个想法是先单击主要类别,然后slideToggle插入并加载子类别。 I have the principles built and was looking at jQuery .children 我已经建立了原理,并正在研究jQuery .children

However when I am clicking the link, Nothing is happening. 但是,当我单击链接时,没有任何反应。

My jQuery is as follows : 我的jQuery如下:

$('ul.sub_menu').css('display','none');

$(".nav-list li a.clickable").toggle(function () {
  $(this).children().slideToggle();
}, function() {
  $(this).children().slideToggle();
}

My HTML Structure Goes Like This... 我的HTML结构是这样的...

<ul class="nav nav-list">
  <li>

    <a class="clickable" href="#">Christmas</a>

    <ul class="sub_menu">
      <li>
        <a href="/~sweetdis/category/wreaths" >Wreaths</a>
      </li>
      <li>
        <a href="/~sweetdis/category/large-logs">Large Logs</a>
      </li>
    </ul>

  </li>
</ul>

Am I just missing something here? 我只是在这里想念东西吗?

Many Thanks 非常感谢

Try this - 尝试这个 -

$('.sub_menu').hide();
$('.clickable').click(function(){
    $(this).next('ul').slideToggle();
});

Or - 要么 -

$('.ulClass li a:first').click(function(){
    $(this).next('ul').slideToggle();
});

Working Demo 工作演示

Nothing is happening when you click on clickable because it doesn't have any children 当您单击clickable时没有任何反应,因为它没有任何子项

Because you use slideToggle for the children of the anchor with class clickable when there is no children of this anchor exist, you can use siblings instead: 因为当不存在该锚点的子级时,您将slideToggle用于具有类clickableanchor的子级,因此可以使用siblings

$('ul.sub_menu').css('display','none');

$(".nav-list li a.clickable").click(function () {
    $(this).siblings('ul.sub_menu').slideToggle();
});​

FIDDLE: http://jsfiddle.net/QqMks/ 内容: http : //jsfiddle.net/QqMks/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM