javascript - How to select only the first element after class? -
i have dropdown menu. want dynamic can tell li
(in nav
) dropdown , give list afterwards dropdown content. problem when click on link .dropdown
class shows elements .dropdown-content
class. idea of smart selector work here?
my html:
<li><a class="dropdown" href="#">gallery</a> <ul class="dropdown-content"> <li><a href="#">photos on me</a></li> <li><a href="#">photos of me</a></li> <li><a href="#">photos me</a></li> </ul> </li> <li><a class="dropdown" href="#">blog</a> <ul class="dropdown-content"> <li><a href="#">photos on me</a></li> <li><a href="#">photos of me</a></li> <li><a href="#">photos me</a></li> </ul> </li>
my js:
var main = function() { $('.dropdown').click(function(){ $('.dropdown + .dropdown-content').toggle(200); }); }
get element on click event has happened, , find siblings class looking for.
$('.dropdown').click(function(){ $(this).siblings('.dropdown-content').toggle(200); });
or
$('.dropdown').click(function(){ $(this).next().toggle(200); });
Comments
Post a Comment