[JQuery]学习——高级选择器

第一,后代选择器

$('#box p').css('color','red');

$('#box').find('p').css('color','red');


<!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=utf-8" />
<title>基础核心</title>
<script type="text/javascript" src="jquery-1.10.1.js"></script>
<script type="text/javascript" src="demo.js"></script>
<link rel="stylesheet" href="style.css" type="text/css"/>
</head>
<body>
<div id="box">
<p>pq</p>
<p>pq</p>
<p>pq</p>
<div>
<p>pq</p>
<p>pq</p>
<p>p</p>
</div>
</div>
</body>
</html>

p全部红色


第二,子代选择器

$('#box > p').css('color','blue');
$('#box').children('p').css('color','blue');

<!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=utf-8" />
<title>基础核心</title>
<script type="text/javascript" src="jquery-1.10.1.js"></script>
<script type="text/javascript" src="demo.js"></script>
<link rel="stylesheet" href="style.css" type="text/css"/>
</head>

<body>
<div id="box">
<p>pq</p>
<p>pq</p>
<p>pq</p>
<div>
<p>pq</p>
<p>pq</p>
<p>p</p>
</div>
</div>
</body>
</html>

第三,next选择器,必须紧挨着

$('#box').next().css('color','blue');


第四,nextAll选择器

$('#box ~ p').css('color', 'red'); //兼容IE6
jQuery 为nextAll 选择器提供了一个等价的方法nextAll():
$('#box').nextAll('p').css('color', 'red'); //和nextAll 选择器等价


第五,prev,prevAll选择器

为了补充高级选择器的这三种模式,jQuery 还提供了更加丰富的方法来选择元素:
$('#box').prev('p').css('color', 'red'); //同级上一个元素
$('#box').prevAll('p').css('color', 'red'); //同级所有上面的元素


第六,上下同级所有

$('#box').siblings('p').css('color', 'red'); //同级上下所有元素选定

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章