在下面的代码中,我正在做菜单。
我要做的是,当用户点击菜单项时,菜单项类需要更改为“当前”,带有“当前”的菜单项需要更改为“菜单”。
但我无法让事情发挥作用。
好的...当前变为菜单。我可以通过颜色来判断。但是,当我点击从“当前”更改为“菜单”的那个时,警报将不会出现。
另外,如何根据点击的数据Feed名称将菜单类设置为当前?你能看出这是怎么做到的吗?
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>PHP, jQuery search demo</title>
<style type="text/css">
.current{
padding:5px;
border:1px solid #000000;
background-color:white;
}
.menu{
padding:5px;
border:1px solid #000000;
background-color:red;
}
</style>
<script type="text/javascript" src="http://localhost/dev/ajax/jquery.js"></script>
<script type="text/javascript">
$(function() {
$(".menunavi .menu").click(function() {
alert ($(this).attr("data-feed"));
//Set the span with class="current" to class="menu"
$(".menunavi .current").addClass("menu");
//Set the span with class="menu" to class="current" were data-feed = 'whatever was clicked'
$(".menunavi .menu").addClass("current");
});
});
</script>
</head>
<body>
<div class="navigation clearfix">
<div class="menunavi">
<span class="current" data-feed="photos">Photos</span>
<span class="menu" data-feed="watches">Watches</span>
<span class="menu" data-feed="audio">Audio</span>
<span class="menu" data-feed="sports">Sports</span>
<span class="menu" data-feed="drinks">Drinks</span>
<span class="menu" data-feed="furniture">Furniture</span>
<span class="menu" data-feed="computers">Computers</span>
<span class="menu" data-feed="stationary">Stationary</span>
<span class="menu" data-feed="phones">Phone</span>
<span class="menu" data-feed="cosmetics">Cosmetics</span>
</div>
</div>
</body>
</html>
答案 0 :(得分:3)
$('.menu,.current').click(function(){
$(this).siblings().removeClass('current').addClass('menu');
$(this).removeClass('menu').addClass('current');
});
这是一个jsfiddle http://jsfiddle.net/N4Gec/2/