0

I have a simple drop down menu. When you click .drop-down, a sub menu slides down. However, if you click any of the children of .drop-down, it slides back up again. I want only the .drop-down that was clicked to slide the menu, none of the its descendents.

Here it is in action: http://jsfiddle.net/tmyie/uXn5k/2/

<ul>
    <li class="drop-down">
        <a href="#"> Main </a> 
        <ul class="sub-menu">
          <li><a href="#">Sub</a> </li>
          <li><a href="#">Sub</a> </li>
          <li><a href="#">Sub</a> </li>
        </ul>
    </li>
    <li><a href="#">Main</a> </li>
    <li><a href="#">Main</a> </li>
    <li><a href="#">Main</a> </li>
    <li><a href="#">Main</a> </li>
</ul>

jQuery

$( ".drop-down" ).click(function() {
  $('.sub-menu').slideToggle();
});

$('.drop-down').fadeTo('slow', 0.3);

http://jsfiddle.net/tmyie/uXn5k/2/

4

1 回答 1

2

专门a针对

$( ".drop-down>a" ).click(function() {
  $('.sub-menu').slideToggle();
});

$('.drop-down').fadeTo('slow', 0.3);

或者,只需将课程lia

<ul>
    <li>
        <a href="#" class="drop-down"> Main </a> 
        <ul class="sub-menu">
        <li><a href="#">Sub</a> </li>
        <li><a href="#">Sub</a> </li>
        <li><a href="#">Sub</a> </li>
    </ul></li>
    <li><a href="#">Main</a> </li>
    <li><a href="#">Main</a> </li>
    <li><a href="#">Main</a> </li>
    <li><a href="#">Main</a> </li>
</ul>

编辑:

顺便说一句,您将在使用此脚本的多个下拉菜单中遇到麻烦。考虑进行以下更改:

   $( ".drop-down>a" ).on("click", function() {
      $(this).siblings('.sub-menu').slideToggle();
    });

    $('.drop-down').fadeTo('slow', 0.3);

http://jsfiddle.net/uXn5k/6/

于 2013-11-07T14:30:32.160 回答