javascript - making a verticle sliding menu with jquery -
here's html:
<ul id="dmenu"> <li><a href="#">menu item one</a></li> <li><a href="#">menu item two</a> <ul class="displaynone"> <li><a href="#">menu item one</a> </ul> </li> </ul>
when user clicks on 1 of tags, want code check wether there ul tags in parent li tag. if there are, want preventdefault action tag , slidetoggle child ul element
my jquery atm:
$('#dmenu > a').click(function(e) { if (this.parent().has('ul')) { e.preventdefault(); $(this).children().slidetoggle(); } });
you doing slide toggle on child of a
change $(this).children().slidetoggle();
to
$(this).parent().children('ul').slidetoggle();
also selector #dmenu > a
wrong - top level menu doesn't have id , if did there no direct anchors children of ul - remove >
, add id dmenu
top ul
or container element
edit
as pointed out austin, have nested lists may want limit click events top level can use #dmenu > li a
Comments
Post a Comment