---
title: "JQuery学习(2)-如何访问DOM对象"
author: "baggio1024"
date: "2009-12-15T14:19:49Z"
tags: []
categories: ["Web前端"]
slug: "jquery-2-dom"
draft: false
lastmod: "2009-12-15T15:26:11Z"
id: 548896
url: "https://www9.iteye.com/blog/548896"
---

jQuery选择器

 

 

示例1 、为超链接增加不同的样式：

```
	<script type="text/javascript" src="jquery/jquery-1.3.2.js" ></script>
	<script type="text/javascript">
	$(document).ready(function() {//增加不同的样式
		$("a[href ^= 'mailto:']").addClass("mailto");	//href以mailto开头的
		$("a[href $= '.pdf']").addClass('pdflink');		//href以.pdf开头的
		$("a[href *= 'mysite']").addClass("mysite");	//href包含mysite的		
	});
```

 

 

### 1. 基础选择器 **Basics**

 

**名称** **说明** **举例** [**#id**](http://docs.jquery.com/Selectors/id#id) 根据元素Id选择 $("divId") 选择ID为divId的元素 [**element**](http://docs.jquery.com/Selectors/element#element) 根据元素的名称选择, $("a") 选择所有&lt;a&gt;元素 [**.class**](http://docs.jquery.com/Selectors/class#class) 根据元素的css类选择 $(".bgRed") 选择所用CSS类为bgRed的元素 [**\***](http://docs.jquery.com/Selectors/all) 选择所有元素 $("\*")选择页面所有元素 [**selector1,**   
**selector2,**   
**selectorN**](http://docs.jquery.com/Selectors/multiple#selector1selector2selectorN) 可以将几个选择器用","分隔开然后再拼成一个选择器字符串.会同时选中这几个选择器匹配的内容. $("#divId, a, .bgRed")

 

2.层次选择器 **Hierarchy**

 

**名称** **说明** **举例** [**ancestor descendant**](http://docs.jquery.com/Selectors/descendant#ancestordescendant) 使用"form input"的形式选中form中的所有input元素.即ancestor(祖先)为from, descendant(子孙)为input. $(".bgRed div") 选择CSS类为bgRed的元素中的所有&lt;div&gt;元素. [**parent &gt; child**](http://docs.jquery.com/Selectors/child#parentchild) 选择parent的直接子节点child.  child必须包含在parent中并且父类是parent元素. $(".myList&gt;li") 选择CSS类为myList元素中的直接子节点&lt;li&gt;对象. [**prev + next**](http://docs.jquery.com/Selectors/next#prevnext) prev和next是两个同级别的元素. 选中在prev元素后面的next元素. $("#hibiscus+img")选在id为hibiscus元素后面的img对象. [**prev ~ siblings**](http://docs.jquery.com/Selectors/siblings#prevsiblings)  选择prev后面的根据siblings过滤的元素   
注:siblings是过滤器 $("#someDiv~\[title]")选择id为someDiv的对象后面所有带有title属性的元素

 

### 3.基本过滤器 **Basic Filters**

 

**名称** **说明** **举例** [**:first**](http://docs.jquery.com/Selectors/first) 匹配找到的第一个元素 查找表格的第一行:$("tr:first") [**:last**](http://docs.jquery.com/Selectors/last) 匹配找到的最后一个元素 查找表格的最后一行:$("tr:last") [**:not(selector)**](http://docs.jquery.com/Selectors/not#selector) 去除所有与给定选择器匹配的元素 查找所有未选中的 input 元素: $("input:not(:checked)") [**:even**](http://docs.jquery.com/Selectors/even) 匹配所有索引值为偶数的元素，从 0 开始计数 查找表格的1、3、5...行:$("tr:even") [**:odd**](http://docs.jquery.com/Selectors/odd) 匹配所有索引值为奇数的元素，从 0 开始计数 查找表格的2、4、6行:$("tr:odd") [**:eq(index)**](http://docs.jquery.com/Selectors/eq#index) 匹配一个给定索引值的元素   
注:index从 0 开始计数 查找第二行:$("tr:eq(1)") [**:gt(index)**](http://docs.jquery.com/Selectors/gt#index) 匹配所有大于给定索引值的元素   
注:index从 0 开始计数 查找第二第三行，即索引值是1和2，也就是比0大:$("tr:gt(0)") [**:lt(index)**](http://docs.jquery.com/Selectors/lt#index) 选择结果集中索引小于 N 的 elements   
注:index从 0 开始计数 查找第一第二行，即索引值是0和1，也就是比2小:$("tr:lt(2)") [**:header**](http://docs.jquery.com/Selectors/header) 选择所有h1,h2,h3一类的header标签. 给页面内所有标题加上背景色: $(":header").css("background", "#EEE"); [**:animated**](http://docs.jquery.com/Selectors/animated) 匹配所有正在执行动画效果的元素 只有对不在执行动画效果的元素执行一个动画特效:

$("#run").click(function(){   
  $("div:not(:animated)").animate({ left: "+=20" }, 1000);   
});

 

### 4. 内容过滤器 Content Filters

 

**名称** **说明** **举例** [**:contains(text)**](http://docs.jquery.com/Selectors/contains#text) 匹配包含给定文本的元素 查找所有包含 "John" 的 div 元素:$("div:contains('John')") [**:empty**](http://docs.jquery.com/Selectors/empty) 匹配所有不包含子元素或者文本的空元素 查找所有不包含子元素或者文本的空元素:$("td:empty") [**:has(selector)**](http://docs.jquery.com/Selectors/has#selector) 匹配含有选择器所匹配的元素的元素 给所有包含 p 元素的 div 元素添加一个 text 类: $("div:has(p)").addClass("test"); [**:parent**](http://docs.jquery.com/Selectors/parent) 匹配含有子元素或者文本的元素 查找所有含有子元素或者文本的 td 元素:$("td:parent")

 

### 5.可见性过滤器  **Visibility Filters**

 

**名称** **说明** **举例**

[**:hidden**](http://docs.jquery.com/Selectors/hidden)

匹配所有的不可见元素

注:在1.3.2版本中, hidden匹配自身或者父类在文档中不占用空间的元素.如果使用CSS visibility属性让其不显示但是占位,则不输入hidden.

查找所有不可见的 tr 元素:$("tr:hidden") [**:visible**](http://docs.jquery.com/Selectors/visible) 匹配所有的可见元素 查找所有可见的 tr 元素:$("tr:visible")

### 6.属性过滤器 **Attribute Filters**

 

**名称** **说明** **举例** [**\[attribute\]**](http://docs.jquery.com/Selectors/attributeHas#attribute) 匹配包含给定属性的元素 查找所有含有 id 属性的 div 元素:   
$("div\[id]") [**\[attribute=value\]**](http://docs.jquery.com/Selectors/attributeEquals#attributevalue) 匹配给定的属性是某个特定值的元素 查找所有 name 属性是 newsletter 的 input 元素:  
$("input\[name='newsletter']").attr("checked", true); [**\[attribute!=value\]**](http://docs.jquery.com/Selectors/attributeNotEqual#attributevalue) 匹配给定的属性是不包含某个特定值的元素 查找所有 name 属性不是 newsletter 的 input 元素:   
$("input\[name!='newsletter']").attr("checked", true); [**\[attribute^=value\]**](http://docs.jquery.com/Selectors/attributeStartsWith#attributevalue) 匹配给定的属性是以某些值开始的元素 $("input\[name^='news']") [**\[attribute$=value\]**](http://docs.jquery.com/Selectors/attributeEndsWith#attributevalue) 匹配给定的属性是以某些值结尾的元素 查找所有 name 以 'letter' 结尾的 input 元素:   
$("input\[name$='letter']") [**\[attribute\*=value\]**](http://docs.jquery.com/Selectors/attributeContains#attributevalue)

匹配给定的属性是以包含某些值的元素

查找所有 name 包含 'man' 的 input 元素:   
$("input\[name\*='man']")

[**\[attributeFilter1\]\[attributeFilter2\]\[attributeFilterN\]**](http://docs.jquery.com/Selectors/attributeMultiple#attributeFilter1attributeFilter2attributeFilterN) 复合属性选择器，需要同时满足多个条件时使用。 找到所有含有 id 属性，并且它的 name 属性是以 man 结尾的:   
$("input\[id]\[name$='man']")

### **7.子元素过滤器 Child Filters**

 

**名称** **说明** **举例** [**:nth-child(index/even/odd/equation)**](http://docs.jquery.com/Selectors/nthChild#index)

匹配其父元素下的第N个子或奇偶元素

':eq(index)' 只匹配一个元素，而这个将为每一个父元素匹配子元素。:nth-child从1开始的，而:eq()是从0算起的！

可以使用:   
nth-child(even)   
:nth-child(odd)   
:nth-child(3n)   
:nth-child(2)   
:nth-child(3n+1)   
:nth-child(3n+2)

在每个 ul 查找第 2 个li:   
$("ul li:nth-child(2)") [**:first-child**](http://docs.jquery.com/Selectors/firstChild)

匹配第一个子元素

':first' 只匹配一个元素，而此选择符将为每个父元素匹配一个子元素

在每个 ul 中查找第一个 li:   
$("ul li:first-child") [**:last-child**](http://docs.jquery.com/Selectors/lastChild)

匹配最后一个子元素

':last'只匹配一个元素，而此选择符将为每个父元素匹配一个子元素

在每个 ul 中查找最后一个 li:   
$("ul li:last-child") [**:only-child**](http://docs.jquery.com/Selectors/onlyChild)

如果某个元素是父元素中唯一的子元素，那将会被匹配

如果父元素中含有其他元素，那将不会被匹配。

在 ul 中查找是唯一子元素的 li:   
$("ul li:only-child")

### 8.表单选择器 Forms

 

**名称** **说明** **解释** [**:input**](http://docs.jquery.com/Selectors/input) 匹配所有 input, textarea, select 和 button 元素 查找所有的input元素:   
$(":input") [**:text**](http://docs.jquery.com/Selectors/text) 匹配所有的文本框 查找所有文本框:   
$(":text") [**:password**](http://docs.jquery.com/Selectors/password) 匹配所有密码框 查找所有密码框:   
$(":password") [**:radio**](http://docs.jquery.com/Selectors/radio) 匹配所有单选按钮 查找所有单选按钮 [**:checkbox**](http://docs.jquery.com/Selectors/checkbox) 匹配所有复选框 查找所有复选框:   
$(":checkbox") [**:submit**](http://docs.jquery.com/Selectors/submit) 匹配所有提交按钮 查找所有提交按钮:   
$(":submit") [**:image**](http://docs.jquery.com/Selectors/image)

匹配所有图像域

匹配所有图像域:   
$(":image") [**:reset**](http://docs.jquery.com/Selectors/reset) 匹配所有重置按钮 查找所有重置按钮:   
$(":reset") [**:button**](http://docs.jquery.com/Selectors/button) 匹配所有按钮 查找所有按钮:   
$(":button") [**:file**](http://docs.jquery.com/Selectors/file) 匹配所有文件域 查找所有文件域:   
$(":file")

### **9.表单过滤器 Form Filters**

 

**名称** **说明** **解释** [**:enabled**](http://docs.jquery.com/Selectors/enabled)

匹配所有可用元素

查找所有可用的input元素:   
$("input:enabled") [**:disabled**](http://docs.jquery.com/Selectors/disabled) 匹配所有不可用元素 查找所有不可用的input元素:   
$("input:disabled") [**:checked**](http://docs.jquery.com/Selectors/checked) 匹配所有选中的被选中元素(复选框、单选框等，不包括select中的option) 查找所有选中的复选框元素:   
$("input:checked") [**:selected**](http://docs.jquery.com/Selectors/selected) 匹配所有选中的option元素 查找所有选中的选项元素:   
$("select option:selected")