html - Javascript Hover Issue on an Image -
i'm trying create effect javascript.
effect:
when click on menu show active image symb1.png. following code:
css:
<style> li{display:inline;padding:0 10px; z-index:-1;} .red{ background-image:url('symb1.png'); background-repeat:no-repeat;} </style>
html in body tag
<ul id="menu"> <li><a href="#" class="red" onclick="selected(this)">menu</a></li> <li><a href="#" onclick="selected(this)">menu 1</a></li> <li><a href="#" onclick="selected(this)">menu 2</a></li> </ul>
finally javasript code before closing html body tag:
<script> function selected(obj){ var lilist = document.getelementbyid('menu'); var alist = lilist.getelementsbytagname('a'); (i=0; i<alist.length; i++ ) { alist[i].classname=""; } obj.classname="red"; } </script>
it's working fine text menu, menu 1, menu 2 etc. if put image instead of text doesn't work. doesn't show active image. why happen?
update:
<html> <head> <style type="text/css"> li{display:inline;padding:0 10px; z-index:-1;} .red{ background-image:url(symb1.png); background-repeat:no-repeat;} </style> </head> <body> <ul id="menu"> <li><a href="#" class="red">menu</a></li> <li><a href="#" onclick="selected(this)">menu 1</a></li> <li><a href="#" onclick="selected(this)">menu 2</a></li> </ul> <script> $('ul li a').click(function(){ $('ul li a').removeclass('red'); $(this).addclass('red'); }); </script> </body> </html>
not sure if open jquery here simple solution. also, here jsfiddle can see in action:
jquery:
$(document).ready(function() { $('ul li a').click(function(){ $('ul li a').removeclass('red'); $(this).addclass('red'); }); });
the rest of code stays same. of course you'll need include jquery library in head of document:
<script src="//http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
Comments
Post a Comment