Dynamic menu items (change text on click)
i need some help with a menu. See the following menu:
menu
The code for this menu:
<div id="menu">
<ul>
<li class="home"><a href="#home" class="panel">home / <span class="go">you are here</span></a></li>
<li class="about"><a href="#about" class="panel">about / <span class="go">go here</span></a></li>
<li class="cases"><a href="#cases" class="panel">cases / <span class="go">go there</span></a></li>
<li class="photos"><a href="#photos" class="panel">photos / <span class="go">maybe here</span></a></li>
<li class="contact"><a href="#contact" class="panel">contact / <span class="go">or even here<span></span></a></li>
</ul>
</div>
What i want to do: onclick a menu item: 1. change the red text to yellow 'you are here' 2. change the previous menu item back to its original state (eg red and "go here").
The 4 values "go here", "go there", "maybe here", "or even here" are the 4 values that should be assigned to the other menu items (like the example).
This is the code i already have:
$('#menu ul li.home').addClass('active')
$('#menu ul li.active a .go').html("you are here");
$("#menu ul li").click(function () {
$('#menu ul li.active').removeClass('active');
$(this).addClass('active');
$('#menu ul li.active a .go').html("you are here");
});
var arr = [ "go here",开发者_Python百科 "go there", "maybe here", "or even here" ];
var obj = { item1: "go here", item2: "go there" ,item3: "maybe here", item4: "or even here"};
$('#menu ul li').click(function () {
var str = $('#menu ul li.active a .go').text();
$('#menu ul li.active a .go').html(str);
});
As you see, it's incomplete. I don't how to get the values from the array and assign them too a menu item. The replace text works, but not the change-back-to-original-state. Also, right now, for some reason i can't click ONTO the list item itself in order to activate the jquery code. I need to click just a few pixels under it. But i guess that's a css issue.
If anyone can help, i'd be super thankful!
Regards,
Mathijs
This should work:
var msgs = [ "go here", "go there", "maybe here", "or even here" ];
var msgs_length = msgs.length;
$("#menu ul li").click(function () {
$('#menu ul li.active').removeClass('active');
$(this).addClass('active');
$('.go', this).text("you are here");
$("#menu ul li").not(this).each(function(i) {
$('.go', this).text(msgs[i % msgs_length]);
});
});
Explanation:
- Use
text()
instead of.html()
if you want to set text only $('.go', this)
will find any element with classgo
inside the current element (read more about selector context)$("#menu ul li").not(this)
selects allli
elements besides the current one (read more about.not()
)i
is the index of the element in the list of the selected elements (read more about.each()
)i % msgs_length
(modulo) ensures that you always have a valid index for the message array (in case there are more menu items than messages)
I don't know if the color thing already works, but this is only a CSS issue:
#menu ul li .go {
color: #FF0;
}
#menu ul li.active .go {
color: #F00;
}
Update:
Btw instead of "manually" setting the value for the home list entry:
$('#menu ul li.home').addClass('active');
$('#menu ul li.active a .go').html("you are here");
consider to simulate a click so that the values for the other list elements are correctly set:
$('#menu li.home').click();
Update2:
To fix the "have-to-click-below" issue ;)
$("#menu ul li a").click(function () {
$('#menu ul li.active').removeClass('active');
$(this).parent().addClass('active');
$('.go', this).text("you are here");
$("#menu ul li a").not(this).each(function(i) {
$('.go', this).text(msgs[i % msgs_length]);
});
});
This is the css
#menu {
position: fixed;
top: 120px;
left:40px;
z-index: 40;
font-family: Arial, sans-serif;
font-size: 0.6em;
text-transform: uppercase;
}
#menu ul li {
float: right;
}
#menu ul li a{
background-color: #292929;
display: block;
padding: 12px 6px 4px 6px;
text-align: right;
margin-bottom: 9px;
color: #f5f5f5;
text-decoration: none;
}
#menu ul li.active a .go{
color: #e4d555;
}
#menu ul li a:hover .go{
color: #e4d555;
}
#menu ul li a:hover{
text-decoration: none;
}
#menu ul li .go{
font-family: Georgia, Serif;
color: #ff0000;
text-transform: lowercase;
font-size:11px;
}
精彩评论