jquery学习系列4(选择器)

1.迭代选择器

1 <script language="javascript">
2  // $("p").click(function() { alert("我是p"); });//这时候DOM元素还没有解析出来呢,所以$("p")取不到任何元素,和讲DOM时候为什么要把初始化事件加到onload或initEvent()一样的。
3   $(function() {
4 $("p").click(function() { alert("我是p"); });//隐式迭代,给所有选择出来的元素添加click事件响应
5   })
6 $(function() {
7 $("#btnok").click(function() {
8 $("p").html("this is a p");
9 })
10 })
11 </script>

2.css选择器

1 <style type="text/css">
2 .test
3 {
4 background-color: Red;
5 color: Blue;
6 }
7 </style>
8
9 <script language="javascript">
10 $(function() {
11 $(".test").click(function() {//css选择器对类class=test添加click事件
12   alert($(this).text());
13 })
14 })
15 </script>
1 <input type="button" id="btnok" value="tagname" />
2 <p>
3 aa</p>
4 <p>
5 bb</p>
6 <p>
7 cc</p>
8 <p class="test">
9 aa</p>
10 <p class="test">
11 bb</p>
12 <p class="test">
13 cc</p>

3.层次选择器

$("div li")获取div下所有的li元素(子代、后代) 

$("div>li")获取div下直接li元素          

$(".mtx+div")获取样式名为.mtx之后的第一个div(不常用)

$(".mtx~div")获取样式名为.mtx之后的所有div(不常用)

posted @ 2011-05-06 09:45  aspneteye  阅读(111)  评论(0编辑  收藏  举报