---
title: "jQuery选择器介绍"
author: "yaasshole"
date: "2012-02-29T22:55:00Z"
tags: []
slug: "jquery"
draft: false
lastmod: "2012-03-03T11:07:06Z"
id: 1436865
url: "https://yaasshole.iteye.com/blog/1436865"
---

**jQuery选择器介绍**

jQuery选择器分为:

#id,.class,element, selector1,selector2,selector3.....

以以下的html为例：

&lt;body&gt;

用户名：&lt;input type=*"text"* id=*"name"* class=*"myCalss"*/&gt;

性别：&lt;input type=*"text"* name=*"sex"* class=*"myCalss"*/&gt;

年龄：&lt;input type=*"text"* name=*"age"* class=*"myCalss"*/&gt;

薪资：&lt;input type=*"text"* name=*"salary"* class=*"myCalss"*/&gt;

出生日期：：&lt;input type=*"text"* name=*"birth"*/&gt;

电话：&lt;input type=*"text"* name=*"phone"* id=*"phone"*/&gt;

&lt;div class=*"myClass"* style=*"width:100,height:100,border="*1px"&gt;

&lt;/div&gt;

&lt;/body&gt;

**1. id选择器**

//1.id选择器，根据它来获取指定的jQuery对象

//$("#id名称")获取指定的id的jQuery对象

**var** $uName = $("#name");//jQuery对象 是一个数组对象

//调用jQuery对象的方法 name名称 value值

$uName.css("color","red");//使得输入框的文字为red

$uName.css("border-color","blue");//使得文本框的元素为blue

2**．emement选择器** 元素节点 ---标签 获取到的是所有的指定的标签

**var** $inputs = $("input");//返回jQuery对象 是一个数组对象

//alert($inputs.size());//长度为6

//jQuery对象的遍历

$inputs.each(**function**(i,ipt){ //i相当于是for循环中的i 并且i++

//ipt是一个dom对象

**if**(i%2==0){

ipt.style.background="yellow";

}**else**{

ipt.style.background="blue";

}

});

**3．class选择器** 根据class获取jQuery对象

**var** $inpCls = $(".myCalss");

$inpCls.css("background-color","pink");

//遍历该对象

$inpCls.each(**function**(i,domCls){

**if**(i%2==0){

domCls.style.background="yellow";

}**else**{

domCls.style.background="blue";

}

});

**4.selector1,selector2,selector3.....多向选择器**

//是返回所有元素的一个jQuery对象

**var** $sels = $(".myClass,#phone");

alert($sels.size());