<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>jquery实现简单的折叠效果</title>
<style type="text/css">
#menu {
width:300px;
}
.has_children{
background : #555;
color :#fff;
cursor:pointer;
}
.highlight{
color : #fff;
background : green;
}
div{
padding:0;
}
div a{
background : #888;
display : none;
float:left;
width:300px;
}
</style>
<!-- 引入 jQuery -->
<script type="text/javascript" src="jquery.min.js"></script>
</head>
<body>
<div id="menu">
<div class="has_children">
<span>第1章-认识jQuery</span>
<a>1.1-JavaScript和JavaScript库</a>
<a>1.2-加入jQuery</a>
</div>
<div class="has_children">
<span>第2章-jQuery选择器</span>
<a>2.1-jQuery选择器是什么</a>
<a>2.2-jQuery选择器的优势</a>
</div>
<div class="has_children">
<span>第3章-jQuery中的DOM操作</span>
<a>3.1-DOM操作的分类</a>
<a>3.2-jQuery中的DOM操作</a>
</div>
</div>
<p id="showtip">select for it"</p>
<div>
<select id="selectTest" name="selectTest">
<option value="1">11</option>
<option value="2">22</option>
<option value="33">33</option>
</select>
</div>
<script type="text/javascript">
//等待dom元素加载完毕.
$(document).ready(function(){
$("#selectTest").change(function()
{
alert($("#selectTest").val()); //下拉值
alert($("#selectTest").find("option:selected").text());
$("#showtip").text($("#selectTest").find("option:selected").text());
});
$(".has_children").click(function(){
if ($(this).hasClass("highlight") ) {
$(this).removeClass("highlight").children("a").hide();
}else{
$(this).addClass("highlight") //为当前元素增加highlight类
.children("a").show().end() //将子节点的a元素显示出来并重新定位到上次操作的元素
.siblings().removeClass("highlight") //获取元素的兄弟元素,并去掉他们的highlight类
.children("a").hide();
}
});
});
</script>
</body>
</html>