$(document).ready(function(){ $('.dropb').hide(); $('.menu-item').bind('click', function(){ $(this).find('.dropb').css("display","block"); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <li> <a class="menu-item" href="#">Π£Ρ‚Π΅ΠΏΠ»Π΅Π½ΠΈΠ΅ ΠΈ ΠΎΡ‚Π΄Π΅Π»ΠΊΠ° Π±Π°Π»ΠΊΠΎΠ½Π°</a> <ul class="dropb"> <li><a href="">fdsfdfsf</a></li> <li><a href="">fdsfdfsf</a></li> <li><a href="">fdsfdfsf</a></li> </ul> </li> 

I would like the slide toggl when pressed, but I think that the selector is not correctly set.

Please tell me what the problem is, why does $(this) not work?

PS: jquery hooked up

  • on css ("display", "block"); do not pay attention, I just experimented, there is a slideToggle (); - Beka Dosniyazov
  • in general, this function should write $ (this) .find ('. dropb'). css ({"display": "block"}); - perfect
  • bind ? What version of jQuery? It has long been necessary to use on - Qwertiy ♦

5 answers 5

 $(document).ready(function() { $('.menu-item').click(function() { $('.dropb').fadeOut(0); // прячСм ΠΎΡ‚ΠΊΡ€Ρ‹Ρ‚Ρ‹Π΅ Π±Π»ΠΎΠΊΠΈ $(this).children('.dropb').toggle(100); }); }); 
 .dropb { display: none; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <li> <div class="menu-item"> <a href="#">Π£Ρ‚Π΅ΠΏΠ»Π΅Π½ΠΈΠ΅ ΠΈ ΠΎΡ‚Π΄Π΅Π»ΠΊΠ° Π±Π°Π»ΠΊΠΎΠ½Π°</a> <ul class="dropb"> <li><a href="">fdsfdfsf</a></li> <li><a href="">fdsfdfsf</a></li> <li><a href="">fdsfdfsf</a></li> </ul> </div> </li> <li> <div class="menu-item"> <a href="#">Π£Ρ‚Π΅ΠΏΠ»Π΅Π½ΠΈΠ΅ ΠΈ ΠΎΡ‚Π΄Π΅Π»ΠΊΠ° Π±Π°Π»ΠΊΠΎΠ½Π°</a> <ul class="dropb"> <li><a href="">fdsfdfsf</a></li> <li><a href="">fdsfdfsf</a></li> <li><a href="">fdsfdfsf</a></li> </ul> </div> </li> 

  • and how to make other pop-up menus close - Beka Dosniyazov
  • Corrected sample code. I would recommend you initially for dropb prescribe in display: none; styles display: none; and then in the script it will be possible to remove the line $('.dropb').fadeOut(0); at the beginning $('.dropb').fadeOut(0); - Dmitriy Kondratiuk
  • children ? o_O - Qwertiy ♦

Generally speaking, $(this) works for you. It does not work for you .find('.dropb') - because the .dropb elements .dropb not children of .menu-item .

It is necessary to search from the parent:

 $(this).parent().find('.dropb')... 
  • everything is clear, I thought that .menu-item is in the div - perfect
  • that's how? $ (". menu-item"). siblings () ... - perfect
  • @perfect yes, you can too - Pavel Mayorov

 $(document).ready(function() { $('.dropb').hide(); $('.menu-item').on('click', function() { $(this).next().toggle(); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <li><a class="menu-item" href="#">Π£Ρ‚Π΅ΠΏΠ»Π΅Π½ΠΈΠ΅ ΠΈ ΠΎΡ‚Π΄Π΅Π»ΠΊΠ° Π±Π°Π»ΠΊΠΎΠ½Π° </a> <ul class="dropb"> <li><a href="">fdsfdfsf</a> </li> <li><a href="">fdsfdfsf</a> </li> <li><a href="">fdsfdfsf</a> </li> </ul> </li> 

    look at my example

     $(".menu-item").click(function(){ $(this).siblings().toggle(100); }); 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <li> <a class="menu-item" href="#">Π£Ρ‚Π΅ΠΏΠ»Π΅Π½ΠΈΠ΅ ΠΈ ΠΎΡ‚Π΄Π΅Π»ΠΊΠ° Π±Π°Π»ΠΊΠΎΠ½Π°</a> <ul class="dropb"> <li><a href="">fdsfdfsf</a></li> <li><a href="">fdsfdfsf</a></li> <li><a href="">fdsfdfsf</a></li> </ul> </li> 

    just don't add additional subscriptions to the click event on the links.

    • You have an event handler hung on the wrong item - Pavel Mayorov
    • @PavelMayorov, corrected - perfect
    • Now your heading disappears with a drop-down menu :) - Pavel Mayorov
    • and how it should be ?, briefly and substitute the filter) - perfect
    • Of course, the title should not disappear. A drop-down menu - so in general, the author is initially hidden. - Pavel Mayorov

    $(this) in this case is a link element, there is no .dprop inside it.

    Example:

     $(document).ready(function(){ $('.dropb').hide(); $('.menu-item').on('click', function(){ $('.dropb').toggle(); }); }); 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <li> <a class="menu-item" href="#">Π£Ρ‚Π΅ΠΏΠ»Π΅Π½ΠΈΠ΅ ΠΈ ΠΎΡ‚Π΄Π΅Π»ΠΊΠ° Π±Π°Π»ΠΊΠΎΠ½Π°</a> <ul class="dropb"> <li><a href="">fdsfdfsf</a></li> <li><a href="">fdsfdfsf</a></li> <li><a href="">fdsfdfsf</a></li> </ul> </li> 

    • if the code contains several links with a submenu with the class '.dropb', then your code will not be correct ... - C.Raf.T