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

1.基本过滤选择器

2.内容过滤选择器

3.可见性选择器

4.属性选择器

5.子元素过滤选择器

6.表单选择器

**基本过滤选择器**

a. :fitst 选取第一个元素

b. :last选取最后一个元素

c. :not() 选取除指定之外的元素

d. :even 偶数选择器

e. :odd 奇数选择器

f. :eq()选取指定索引的元素

g. :gt()选取大于指定索引的元素

h. :lt()选取小于指定索引的元素

i. :header选取h1,h2,h3,h4,h5,h6标签

j. :animated选取正在执行动画的元素

k. :lang()语言代码选择器，1.9版本新增加，基本用不到

l. :root根元素选择器，1.9版本新增，没什么用

m. :target选择由文档URI格式化识别码表示的目标元素，1.9新增,基本用不到

&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;demo1&lt;/title&gt;  
&lt;script src="jquery-1.10.2.min.js"&gt;&lt;/script&gt;  
&lt;style&gt;  
#go{width: 100px;height: 100px;background: teal;position: absolute;left: 0;top: 0;}  
&lt;/style&gt;  
&lt;/head&gt;  
&lt;body&gt;  
&lt;p&gt;&lt;span&gt;我是&lt;/span&gt;第&lt;em&gt;11111&lt;/em&gt;个标签&lt;/p&gt;  
&lt;p&gt;我是第22222个标签&lt;/p&gt;  
&lt;p class="a"&gt;我是第33333个标签&lt;/p&gt;  
&lt;p&gt;我是第44444个标签&lt;/p&gt;  
&lt;p&gt;我是第55555个标签&lt;/p&gt;

&lt;h1&gt;我是h1标签&lt;/h1&gt;  
&lt;h2&gt;我是h2标签&lt;/h2&gt;  
&lt;h3&gt;我是h3标签&lt;/h3&gt;  
&lt;h4&gt;我是h4标签&lt;/h4&gt;  
&lt;h5&gt;我是h5标签&lt;/h5&gt;  
&lt;h6&gt;我是h6标签&lt;/h6&gt;  
&lt;div id="go"&gt;&lt;/div&gt;  
&lt;script&gt;  
// $('p :first').css('background','red');  
//$('p:last').css('background','red');

// $('p:not(\\'.a\\')').css('background','red');

// $('p:even').css('background','red');  
// $('p:odd').css('background','red');

// $('p:eq(3)').css('background','red');

// $('p:gt(2)').css('background','red');  
// $('p:lt(2)').css('background','red');  
//$(':header').css('background','red');

$('#go').animate({'left':'1000'},10000);

$(':animated').css('background','red');  
&lt;/script&gt;  
&lt;/body&gt;  
&lt;/html&gt;

**内容过滤选择器**

a:contains()选取含有文本内容的元素

b:has()选择指定元素的元素

c:empty选择不包含子元素或者文本空元素

d:parent选取含有子元素或者文本的元素

&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;demo2&lt;/title&gt;  
&lt;script src="jquery-1.10.2.min.js"&gt;&lt;/script&gt;  
&lt;style&gt;  
p{height: 30px;line-height: 30px;}  
&lt;/style&gt;  
&lt;/head&gt;  
&lt;body&gt;  
&lt;p&gt;javascript是一种由Netscape的&lt;strong&gt;LiveScript&lt;/strong&gt;发展而来的原型化继&lt;/p&gt;  
&lt;p&gt;遗留的速度问题，为客户提供更流畅的浏览效果&lt;/p&gt;  
&lt;p&gt;&lt;a href="###"&gt;&lt;/a&gt;&lt;/p&gt;  
&lt;p&gt;&lt;/p&gt;

&lt;script&gt;  
// $('p:contains("LiveScript")').css('background','red');  
//$('p:contains("LiveScript")').show().siblings().hide();

//$('p:has("strong")').css('background','red');

//$('p:empty').css('background','red');  
$('p:parent').css('background','red');  
&lt;/script&gt;

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

**可见性选择器**

a. :hidden选取所有不可见元素

b. :visible选取所有可见元素

&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;demo2&lt;/title&gt;  
&lt;script src="jquery-1.10.2.min.js"&gt;&lt;/script&gt;  
&lt;style&gt;  
p{height: 30px;line-height: 30px;}  
.hide{display: none;/\*opacity: 0;visibility: hidden;\*/}  
&lt;/style&gt;  
&lt;/head&gt;  
&lt;body&gt;  
&lt;p&gt;javascript是一种由Netscape的&lt;strong&gt;LiveScript&lt;/strong&gt;发展而来的原型化继&lt;/p&gt;  
&lt;p&gt;遗留的速度问题，为客户提供更流畅的浏览效果&lt;/p&gt;

&lt;div class="hide"&gt;我是不可见的DIV&lt;/div&gt;  
&lt;div&gt;我是可见的div&lt;/div&gt;

&lt;script&gt;  
//alert($('div:hidden').html());  
alert($('div:visible').html());  
//:hidden选择器的条件  
//1.首先肉眼要在网页上看不到的元素  
//2.这个元素不占有网页上的位置  
&lt;/script&gt;

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

**属性选择器**

a. \[attr]选择拥有此属性的选择器

$('p\[title]').css('background','red');

b. \[attr=value]指定属性值的选择器

$('p\[title=js]').css('background','red');

c. \[attr1]\[attr2]\[attr3]复合属性选择器

$('p\[title]\[myattr=my]').css('background','red');

d. 其它属性选择器（可到jQuery API手册上过一遍）

**子元素过滤选择器**

a. :first-child 选择父元素第一个子元素

$('div p:first-child').css('background','red');

b. :last-child选择父元素最后一个子元素

c.:only-child选择元素中只包含一个子元素的元素

$('div p:only-child').css('background','red');

d.:nth-child(表达式) 选取父元素中的第X个元素，X由表达式决定

$('div p:nth-child(2)').css('background','red');

$('div p:nth-child(even)').css('background','red');

e.:first-of-type选取所有相同元素的第一个兄弟元素 1.9新增

f.:last-of-tyoe选取所有相同元素的最后一个兄弟元素 1.9新增

g.:nth-last-child(表达式)选择所有他们父元素的第n个子元素。计数从最后一个元素开始到第个，1.9+

h.:only-of-type:nth-of-type(表达式):nth-last-of-type(表达式)

表单元素选择器

a :input

b :text

c :password

d :radio

e :checkbox

f :submit

g :image

h :reset

i :button

j :file

k :hidden

l :textarea

m:select

&lt;script&gt;  
//alert($(':input').length);

//alert($(':text').length);  
//$(':text').css('background','red')  
//$(':password').css('background','red')  
//alert($(':radio').val());  
//alert($(':checkbox').val());  
//alert($('input\[type=checkbox]').val());  
alert($('input:hidden').val())  
&lt;/script&gt;

**表单属性选择器**

a :enabled 选择所有可用元素

b :disabled选择所有不可用元素

c :checked选择可有选中元素

d :selected选取所有被选中的选项元素

e :focus选择当前获取焦点的元素 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;demo7&lt;/title&gt;  
&lt;script src="jquery-1.10.2.min.js"&gt;&lt;/script&gt;  
&lt;/head&gt;  
&lt;body&gt;  
&lt;input type="text" name="" id="" value="我是不能修改的文本框" disabled="disabled" /&gt;  
&lt;input type="text" name="" id="" value="我是可以修改的文本框" /&gt;

&lt;input type="checkbox" name="" id="" checked="checked" value="aaa" /&gt;  
&lt;input type="checkbox" name="" id="" value="bbb" /&gt;  
&lt;input type="checkbox" name="" id="" value="ccc" /&gt;

&lt;select name="" id=""&gt;  
&lt;option value="第一个选项"&gt;第一个选项&lt;/option&gt;  
&lt;option value="第二个选项" selected="selected"&gt;第二个选项&lt;/option&gt;  
&lt;option value="第三个选项"&gt;第三个选项&lt;/option&gt;  
&lt;/select&gt;  
&lt;script&gt;  
//alert($('input:disabled').val());  
//alert($('input:enabled').val());  
//  
//alert($('input\[disabled=disabled]').val());

//alert($(':checked').val());  
//alert($('input\[checked=checked]').val());

/\*alert($('select option:selected').val())

alert($(':selected').val())  
alert($('select option\[selected=selected]').val())\*/  
&lt;/script&gt;  
&lt;/body&gt;  
&lt;/html&gt;

下集预告  
1、和选择DOM有关的方法，比如：next(),parent(),parents(),children(),eq().....etc  
2、当选择器中有特殊符号的时候，我们怎么如何处理  
3、实例  
a.先用原生的javascript写一个tab标签页  
b.我们用jquery写一个tab标签页，并利用强大的jquery选择器，使tab标签页实现的代码尽量地少，尽量保持在1~2行代码量之间解决问题