<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>子元素过滤器</title>
<script type="text/javascript" src="../jquery-1.4.1.js"></script>
<script type="text/javascript">
//选择所有div下边的第一个子元素
$(function(){
$('#b1').click(function(){
$('div:nth-child(3)').css('background','#ccc')
})
})
//选取所有div下边的第一个子元素,相当于 div:nth-child(1)
$(function(){
$('#b2').click(function(){
$('div:first-child').css('background','#ccc')
})
})
//选取所有div下边最后子元素
$(function(){
$('#b3').click(function(){
$('div:last-child').css('background','#ccc')
})
})
//选取有唯一子元素的子元素
$(function(){
$('#b4').click(function(){
$('div:only-child').css('background','#ccc')
})
})
</script>
</head>
<body>
<div id="div1" style="width:200px; height:100px; margin:10px; border:1px solid #F00">这是第一个div
<div style=" width:150px; height:50px;border:1px solid #F00;margin:10px">第一个div中的div</div>
</div>
<div id="div2" style="width:200px; height:200px; margin:10px; border:1px solid #F00">这是第er个div
<div style=" width:150px; height:50px;border:1px solid #F00;margin:10px">第er个div中的div</div>
<div style=" width:150px; height:50px;border:1px solid #F00;margin:10px">第er个div中的div</div>
</div>
<div id="div3" style="width:200px; height:220px; margin:10px; border:1px solid #F00">这是第san个div
<div style=" width:150px; height:50px;border:1px solid #F00;margin:10px">第san个div中的div</div>
<div style=" width:150px; height:50px;border:1px solid #F00;margin:10px">第san个div中的div</div>
<div style=" width:150px; height:50px;border:1px solid #F00;margin:10px">第san个div中的div</div>
</div>
<input type="button" value="测试"id="b1"/><input type="button" value="测试"id="b2"/><input type="button"value="测试" id="b3"/><input type="button"value="测试" id="b4"/>
</body>
</html>
联系客服