本文實例講述了jQuery實現導航欄頭部菜單項點擊後變換顏色的方法。分享給大家供大家參考,具體如下:
實現效果如下:


話不多說直接上代碼:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
a{
text-decoration: none;
color: black;
}
#menu{
width: 100%;
height: 20px;
background: gainsboro;
}
ul li{
list-style: none;
float: left;
padding-left: 20px;
background-color: whitesmoke;
}
.active {
color: white;
background-color: black;
}
.none {
background-color: whitesmoke;
}
</style>
</head>
<body>
<ul id="menu">
<li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >首頁</a></li>
<li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >直播</a></li>
<li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >新聞</a></li>
<li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >關於</a></li>
</ul>
<script src="jquery-1.7.2.min.js"></script>
<script>
$('#menu li a').click(function () {
var f = this;
$('#menu li a').each(function () {
this.className = this == f ? 'active' : 'none'
});
});
</script>
</body>
</html>
更多關於jQuery相關內容感興趣的讀者可查看本站專題:《jQuery切換特效與技巧總結》、《jQuery擴展技巧總結》、《jQuery常用插件及用法總結》、《jQuery拖拽特效與技巧總結》、《jQuery表格(table)操作技巧匯總》、《jquery中Ajax用法總結》、《jQuery常見經典特效匯總》、《jQuery動畫與特效用法總結》及《jquery選擇器用法總結》
希望本文所述對大家jQuery程序設計有所幫助。