我有一个代码,当我单击特定元素时,它的背景应该通过删除所有其他元素背景来改变。但是我遇到的问题是,当我在 ul li 下单击 li 时,它的父 li 也将提供背景。请在下面检查我的代码
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title></title>
<meta name="" content="">
<script type="text/javascript" src="jquery-1.3.2.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("ul li ul li").click(function() {
//a = $(this).html(); // when clicking any of these links
//alert(a);
$("*").removeClass("active"); // remove highlight from all links
$(this).addClass("active"); // add highlight to clicked link
})
});
</script>
<script type="text/javascript">
$(document).ready(function(){
$("ul li").click(function() {
//a = $(this).html(); // when clicking any of these links
//alert(a);
$("*").removeClass("active"); // remove highlight from all links
$(this).addClass("active"); // add highlight to clicked link
})
});
</script>
<style>
.active { color:white; background:green; }
</style>
</head>
<body>
<ul>
<li>level1
<ul>
<li>aaa</li>
<li>bbb</li>
<li>ccc</li>
</ul>
</li>
<li>level2</li>
<li>level3</li>
</ul>
</body>
</html>