javascript - <li> active masterpage jQuery not working -
i got menu on mastepage, below:
<div class="navtop"> <ul class="nav"> <li class=""><a href="default.aspx">home</a></li> <li class=""><a href="test.aspx">link</a></li> <li class=""><a href="hello.aspx">link</a></li> <li class=""><a href="world.aspx">link</a></li> </ul> </div>
to set class of <li></li>
active using javascript code:
<script> $("li").click(function () { alert("this message"); if ($("li").hasclass('active')) { $("li").removeclass('active'); } $(this).addclass('active'); }); </script>
this should work, because saw somewhere , should work. doesn't work me, menu stays empty class="" , alert-message javascript (to test it) doesn't work either.
can me? doing wrong?
check havve added jquery , bind event in document.ready event. code seems work. missed closing quotes here class="navtop
adding jquery
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
putting code in document.ready
$(document).ready(function(){ $("li").click(function (event) { event.preventdefault(); if ($("li").hasclass('active')) { $("li").removeclass('active'); } $(this).addclass('active'); }); });
Comments
Post a Comment