我有一个menu.php
如下:
<?php $menu =
'<div id="menu">
<ul>
<a href="home.php"><li>HOME</li></a>
<a href="about.php"><li>ABOUT</li></a>
<a href="works.php"><li>WORKS</li></a>
<a href="clients.php"><li>CLIENTS</li></a>
<a href="contact.php"><li>CONTACT</li></a>
</ul>
</div>';
?>
现在我想让背景和字体颜色与悬停列表时相同。为此,我添加了一个 jQuery 来添加一个名为的类active
,但它不起作用!我的jQuery和CSS:
$(document).ready(function() {
$('#menu ul li').click(function(){
//alert('Active');
$(this).parent().find('li.active').removeClass('active');
$(this).addClass('active');
});
});
CSS:
#menu li:hover
{
background:#ffca00;
color:#2e2f34;
padding:16px 12px;
}
#menu li.active
{
background:#ffca00;
color:#2e2f34;
padding:16px 12px;
}
怎么了 ?我的 jquery 错了吗?
更新:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script src="http://code.jquery.com/jquery-1.9.0.js"></script>
<script>
$(document).ready(function () {
$('#menu ul li').click(function () {
$(this).parent().parent().find('.active').removeClass('active');
a//lert(this);
$('#menu ul li').addClass('active');
});
});
</script>
<style>
#menu
{
width:450px;
margin-left:400px;
background:#000;
}
#menu ul
{
padding:18px;
}
#menu li{
display:inline;
list-style:none;
padding:18px 12px;
text-transform:uppercase;
}
#menu a
{
color:#FFF;
font-family: Candara, Calibri, Segoe, "Segoe UI", Optima, Arial, sans-serif;
font-size:14px;
text-decoration:none;
}
#menu li:hover {
background:#ffca00;
color:#2e2f34;
padding:16px 12px;
}
.active {
font-weight:bold;
background:#ffca00;
color:#2e2f34;
padding:16px 12px;
}
</style>
</head>
<body>
<div id="menu">
<ul> <a href="activemenu.html"><li>HOME</li></a>
<a href="activemenu2.html"><li>ABOUT</li></a>
<a href="activemenu3.html"><li>ABOUT</li></a>
</ul>
</div>
</body>
</html>