---
title: "jQuery学习第三课(选择器第三讲)"
author: "mywebcode"
date: "2014-04-07T09:27:00Z"
tags: []
slug: "jquery"
draft: false
lastmod: "2014-04-19T16:33:07Z"
id: 2049269
url: "https://mywebcode.iteye.com/blog/2049269"
---

﻿﻿

1.DOM筛选，遍历查找相关方法

2.选择器中特殊符号的处理

3.改写原生js例子

a.表格隔行变色

b.tab标签页

4.选择器的优化准则(初级)

**筛选**

eq()选择指定索引的元素

filter(表达式)筛选指定表达式的元素

first()选择第一个元素

last()选择最后一个元素

is()检测是否元素返回布尔值

has()保留包含特定后代的元素，去掉那些不含有指定后代的元素

not()从匹配的元素集合中移除指定的元素。

map()将一组元素转换成其他数组

slice()根据指定的下标范围，选取匹配的元素集合

&lt;script&gt;  
// $('p:eq(0)').css('background','red');  
// $('p').filter('#second').css('background','red');  
// $('p').first().css('background','red');  
// $('p').last().css('background','red');

/* $('p').click(function(){  
if($(this).is('.first')){  
$(this).css('background','red');  
}  
});\*/

// $('p').not('#second').css('background','red');  
//  
// $('p').slice(-2,-1).css('background','red');

&lt;/script&gt;

&lt;body&gt;  
&lt;form action=""&gt;  
&lt;input type="text" value="111" /&gt;  
&lt;input type="text" value="222" /&gt;  
&lt;input type="text" value="333" /&gt;  
&lt;/form&gt;  
&lt;p&gt;&lt;/p&gt;

&lt;script&gt;  
var arr = $('input').map(function(){  
return $(this).val()  
}).get().join(',');

alert(typeof arr);  
$('p').html(arr);  
&lt;/script&gt;  
&lt;/body&gt;

**遍历查找**

a.children()选取子元素

b.parent()选取父元素

c.parents()选取祖先元素

d.parentsUntil()所有的父辈元素，直到遇到匹配的那个元素为止1.6+ //与有参数的parents()等价

e.offsetParent()返回父元素中第一个其position设为relative或者absolute

的元素,仅对可见元素有效

f.next()选择后面紧临的兄弟元素

g.nextAll()查找当前元素之后所有的同辈元素

h.nextUntil()所有的同辈元素，直到遇到匹配的那个元素为止1.6+

i.prev()前一个兄弟元素

j.prevAll()前面所有的兄弟元素

k.prevUntil()所有的兄弟元素，直到遇到匹配的那个元素为止1.6+

&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd](http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd)"&gt;  
&lt;html xmlns="[http://www.w3.org/1999/xhtml](http://www.w3.org/1999/xhtml)" xml:lang="en"&gt;  
&lt;head&gt;  
&lt;meta http-equiv="Content-Type" content="text/html;charset=UTF-8"&gt;  
&lt;title&gt;demo3&lt;/title&gt;  
&lt;script src="jquery.js"&gt;&lt;/script&gt;  
&lt;/head&gt;  
&lt;body&gt;

&lt;div id="outer" style="position:relative"&gt;  
outer  
&lt;div id="wrap" style=""&gt;  
wrap  
&lt;div&gt;111111111&lt;/div&gt;  
&lt;div&gt;222222222&lt;/div&gt;  
&lt;p id="p1"&gt;我是&lt;span&gt;第1个P&lt;/span&gt;标签&lt;/p&gt;  
&lt;p id="p2"&gt;我是第2个P标签&lt;/p&gt;  
&lt;p&gt;我是第3个P标签&lt;/p&gt;  
&lt;div&gt;我是div标签&lt;/div&gt;  
&lt;/div&gt;  
&lt;/div&gt;  
&lt;script&gt;  
// $('#wrap').children('p').css('background','red');  
// $('#p1').parent().css('background','red');  
// $('#p1').parents('#wrap').css('background','red');  
//  
//$('#p1').offsetParent().css('background','red');  
//$('#p1').next().css('background','red');  
//$('#p1').nextAll('p').css('background','red');  
//  
//$('#p2').prev().css('background','red');  
//$('#p2').prevAll('div').css('background','red');  
//  
//$('#p2').siblings('div').css('background','red');  
//  
//$('span').parent().css('background','red').end().css('background','#abcdef');  
//$('span').css('background','#abcdef');  
//  
$('#p1').nextAll().addBack().css('background','red');  
&lt;/script&gt;

&lt;/body&gt;  
&lt;/html&gt;

l.sinlings()前后所有的兄弟元素

m.closest()从元素本身开始，在DOM树上逐级向上级元素匹配，并返回最先匹配的祖先元素

n.contents()元素的子元素，包括文字和注释节点

o.end()终止在当前链的最新过滤操作，并返回匹配的元素的以前状态

p.andself()1.8版本中已废弃

q.addBack()添加堆栈中元素集合到当前集合，一个选择性的过滤选择器

r.each()遍历一个jQuery对象，为每个匹配元素执行一个函数

&lt;script src="jquery.js"&gt;&lt;/script&gt;  
&lt;/head&gt;  
&lt;body&gt;  
&lt;form action=""&gt;  
&lt;input type="text" name="" id="" value="第1个input的值" /&gt;  
&lt;input type="text" name="" id="" value="第2个input的值" /&gt;  
&lt;input type="text" name="" id="" value="第3个input的值" /&gt;  
&lt;input type="text" name="" id="" value="第4个input的值" /&gt;  
&lt;/form&gt;  
&lt;script&gt;  
/\*$('input').each(function(){  
alert($(this).val());  
});\*/  
&lt;/script&gt;

**特殊符号的处理**

使用转义符

&lt;body&gt;  
&lt;form action=""&gt;爱好：  
&lt;input type="checkbox" name="gender\[]" id="" value="看书" /&gt;  
&lt;input type="checkbox" name="gender\[]" id="" value="篮球" /&gt;  
&lt;input type="checkbox" name="gender\[]" id="" value="编程" /&gt;  
&lt;input type="submit" value="提交" /&gt;&lt;input type="reset" value="重写" /&gt;  
&lt;/form&gt;

&lt;script&gt;

/\*$('input\[name=gender\\\\\[\\\\]]').click(function(){  
alert($(this).val());  
});\*/

$('input\[type=\\'checkbox\\']').click(function(){  
alert($(this).val());  
});  
&lt;/script&gt;  
&lt;/body&gt;

**改写实例**

a.表格隔行变色

//js

&lt;script&gt;  
var oTable = document.getElementById('table');  
var aTr = oTable.getElementsByTagName('tr');  
//alert(aTr.length);

for(var i=0;i&lt;aTr.length;i++){

if(i%2==1){  
aTr\[i].style.background="yellow";  
}else{  
aTr\[i].style.background="#abcdef";  
}  
}

&lt;/script&gt;

//jquery

&lt;script&gt;  
/\*$('#table tr:even').css('background','#abcdef');  
$('#table tr:odd').css('background','yellow');\*/

$('#table tr').filter(':even').css('background','#abcdef').end().filter(':odd').css('background','yellow');  
&lt;/script&gt;

b.tab标签页

//js

&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd](http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd)"&gt;  
&lt;html xmlns="[http://www.w3.org/1999/xhtml](http://www.w3.org/1999/xhtml)" xml:lang="en"&gt;  
&lt;head&gt;  
&lt;meta http-equiv="Content-Type" content="text/html;charset=UTF-8"&gt;  
&lt;title&gt;原生js的tab标签页&lt;/title&gt;  
&lt;style&gt;  
\*{  
padding: 0;  
margin: 0;  
}  
ul{  
list-style-type: none;  
}  
body{  
margin: 50px ;  
}  
#ul{  
height: 30px;  
margin-bottom: 10px;  
}  
#ul li{  
height: 30px;  
line-height: 30px;  
padding: 0 15px;  
border: 1px solid #abcdef;  
float: left;  
margin-right: 3px;  
cursor: pointer;  
}  
#ul li.current{  
background: #abcdef;  
}

#content div{  
width: 300px;  
height: 200px;  
border: 1px solid #abcdef;  
display: none;  
}  
#content div.show{  
display: block;  
}  
&lt;/style&gt;  
&lt;/head&gt;  
&lt;body&gt;  
&lt;ul id="ul"&gt;  
&lt;li class="current"&gt;php&lt;/li&gt;  
&lt;li&gt;ruby&lt;/li&gt;  
&lt;li&gt;python&lt;/li&gt;  
&lt;/ul&gt;  
&lt;div id="content"&gt;  
&lt;div class="show"&gt;php。。。。。。介绍&lt;/div&gt;  
&lt;div&gt;ruby。。。。。。介绍&lt;/div&gt;  
&lt;div&gt;python。。。。。。介绍&lt;/div&gt;  
&lt;/div&gt;

&lt;script&gt;  
var ul = document.getElementById('ul');  
var li = ul.getElementsByTagName('li');  
var content = document.getElementById('content');  
var div = content.getElementsByTagName('div');

for (var i = 0; i &lt; li.length; i++) {  
li\[i].index = i;  
li\[i].onclick=function(){  
for (var i = 0; i &lt; li.length; i++) {  
li\[i].className='';  
div\[i].style.display='none';  
};

this.className='current';  
div\[this.index].style.display='block';  
}  
};

&lt;/script&gt;

&lt;/body&gt;  
&lt;/html&gt;

//jquery

&lt;script&gt;  
$('#ul li').click(function(){

//1、点击li的时候要切换样式  
//$(this).addClass('current').siblings().removeClass('current');  
//2、根据li的索引值，来确定哪个div显示，其它div隐藏  
//$('#content&gt;div').eq($(this).index()).show().siblings().hide();

//$(this).addClass('current').siblings().removeClass('current').parent().next().children().eq($(this).index()).show().siblings().hide();

});

$(this).addClass('current').siblings().removeClass('current').parent().next().find('div').eq($(this).index()).show().siblings().hide();

});  
&lt;/script&gt;

**jquery选择器的优化**

1.优先使用id选择器

2.在class选择器前添加标签名

3.采用find(),而不使用上下文查找

4.强大的链式操作比缓存更快

5.从左至右的模型1.3+版本更新

下集预告

jquery操作DOM的方法及实例

﻿﻿