一、基本选择器
<body>
<div>
<div id="div1">
aaaaaaaaaaa</div>
<div class="c1">
bbbbbbbbb</div>
<span>ccccccccc</span>
</div>
</body>
$(function () {

    $("#div1").html("hello world 1"); //根据id匹配元素(a)

    $(".c1").html("hello  world 2"); //根据Yclass匹配元素(b)

    $("span").html("hello world 3"); //根据元素名称匹配 (c)

    $("span,div.c1").html("hello world 4"); //匹配页面所有的span 和class=c1的div(b c)

    $("*").html("hello world 5"); //匹配页面所有元素,也包含body

});

二、层级选择器

<body>
<span id="span1">
aaaaaaaaa</span>
<span class="c1">
bbbbbbbbb</span>
<div>
ccccccccc
<span>ddddddddd</span>
</div>
<div>eeeeeeeee</div>
</body>
$(function () {

    $("body span").html("hello world 1"); //选取body中所有的span(a b d)

    $("body>span").html("hello world 2"); //选取body元素的子span元素(a b)

    $("span.c1+div").html("hello world 3"); //选取class为c1的span的下一个div元素,注意是同级元素
$("span.c1").next().html("hello world 3"); //跟上行效果相同 (c) $("span.c1~div").html("hello world 4"); //选取class为c1的span的后面的所有div
$("span.c1").nextAll().html("hello world 4"); //跟上行效果相同(c e) });

三、基本过滤选择器

<body>
<h1>header1</h1>
<h2>header2</h2>
<h3>header3</h3>
<span id="span1">aaaaaaaaa</span>
<span class="c1">bbbbbbbbb</span>
<div>
ccccccccc
<span>ddddddddd</span>
</div>
<div>eeeeeeeee</div>
</body>
$(function () {

    $("div:first").html("hello world 1"); //选取所有div中的第一个div
$("span:last").html("hello world 2"); //选取所有span中的最后一个
$("span:not(.c1)").html("hello world 3"); //选取除class为c1的span外的所有span
$("span:even").html("hello world 4"); //选取索引为偶数的span,索引从0开始
$("span:odd").html("hello world 5"); //选取索引为奇数的span,索引从0开始
$("span:eq(2)").html("hello world 6"); //选取指定索引的span,索引从0开始
$("span:gt(0)").html("hello world 7"); //选取大于指定索引的span,不包含指定索引
$("span:lt(2)").html("hello world 8"); //选取小于指定索引的span,不包含指定索引
$(":header").html("hello world 9"); //选取页面中所有的标题元素 h1 h2 h3... });

四、内容过滤选择器

<body>
<span id="span1">aaaaaaaaa</span>
<span class="c1">bbbbbbbbb</span>
<span></span>
<div>
ccccccccc
<span id="span2" class="c2">ddddddddd</span>
</div>
<div>eeeeeeeee</div>
</body>
$(function () {

    $("span:contains(aa)").html("hello world 1"); //选取内容包含aa的span元素

    $("span:empty").html("hello world 2"); //选取空的span元素

    $("div:has(span)").html("hello world 3"); //选取包含span的div元素

    $("span:parent").html("hello world 4"); //选取包含子元素的span元素,包含文本

});

五、属性过滤选择器

<body>
<span id="span1">aaaaaaaaa</span>
<span class="c1">bbbbbbbbb</span>
<span></span>
<div>
ccccccccc
<span id="span2" class="c2">ddddddddd</span>
</div>
<div>eeeeeeeee</div>
</body>
$(function () {

    $("span[id]").html("hello world 1"); //选取有属性id的span元素
$("span[id=span2]").html("hello world 2"); //选取属性id等于span2的span元素
$("span[id!=span2]").html("hello world 3"); //选取属性id不等于为span2的span元素
$("span[id^=span]").html("hello world 4"); //选取属性id以span开始的span元素
$("span[id$=2]").html("hello world 5"); //选取属性id以2结尾的span元素
$("span[id*=an]").html("hello world 6"); //选取属性id包含an的span元素
$("span[id*=an][class$=2]").html("hello world 6"); //选取属性id包含an并且class以结尾的span元素 });

六、子元素过滤选择器

<body>
<div class="c1">
<span>aaaaaaaa</span><span>cccccccc</span><span>dddddddd</span>
</div>
<div class="c1">
<span>aaaaaaaa</span><span>cccccccc</span><span>dddddddd</span>
</div>
<div class="c1">
<span>aaaaaaaa</span>
</div>
</body>
$
(function () {

    $("div.c1 :nth-child(1)").html("hello world 1"); //选取class等于c1的div的指定索引子元素
$("div.c1 :nth-child(even)").html("hello world 2"); //选取class等于c1的div的偶数子元素
$("div.c1 :nth-child(odd)").html("hello world 3"); //选取class等于c1的div的奇数子元素
$("div.c1 :first-child").html("hello world 4"); //选取class等于c1的div的第一个子元素
$("div.c1 :last-child").html("hello world 5"); //选取class等于c1的div的最后一个子元素
$("div.c1 :only-child").html("hello world 6"); //选取class等于c1的div只有一个子元素的子元素 });

7、表单选择器

<body>
<form id="form1" action="#">
<input type="button" value="button1" />
<input type="text" value="text1" />
<input type="text" value="text2" />
<textarea rows="8" cols="40"></textarea><br />
<input type="checkbox" name="chk" />篮球
<input type="checkbox" name="chk" />足球
<input type="password" />
<input type="hidden" /><br />
<select multiple="multiple">
<option selected="selected">武汉</option>
<option selected="selected">黄冈</option>
<option >麻城</option>
</select>
<input id="n" type="radio" name="s"/>男
<input type="radio" name="s"/>女<br />
<input type="submit" /><input type="reset" />
</form>
</body>
$(function () {
//表单中的表单元素
$("#form1 :input").val("hello world 1");
//表单中的input元素
$("#form1 input").val("hello world 1");
$(":text").val("hello world 2");
$(":password").val("hello world 3");
//男的单选框被选中
$(":radio[id=n]").attr("checked", true);
$(":checkbox[name=chk]").length;
$(":submmit").val("提交");
$(":reset").val("重置");
$(":button").val("hello world 4");
$(":hidden").val("hello world 5");
});

8、表单对象属性过滤选择器

<body>
<form id="form1" action="#">
<input type="text" disabled="disabled" value="禁用1" />
<input type="text" value="启用1" />
<input type="text" disabled="disabled" value="禁用2" />
<input type="text" value="启用2" />
<input type="checkbox" name="chk" value="篮球" checked="checked"/>篮球
<input type="checkbox" name="chk" value="足球" />足球
<input type="checkbox" name="chk" value="编程" checked="checked"/>编程
<input type="checkbox" name="chk" value="旅游" checked="checked"/>旅游
<select multiple="multiple">
<option selected="selected">武汉</option>
<option selected="selected">黄冈</option>
<option >麻城</option>
</select>
</form></body>
 
$(function () {

    $("#form1 input:enabled").val("hello world 1"); //选取form表单中没有禁用的文本框
$("#form1 input:disabled").val("hello world 2"); //选取form表单中没有禁用的文本框
$("#form1 input:checked").attr("checked",false); //选取form表单中选的复选框
$("select option[selected]").each(function () {
alert($(this).val());
}); });
 
使用选择器要注意的地方
<body>
<div id="div#1">aaaaaaaaaaa</div>
<div class="c[1]">bbbbbbbbb</div>
<div class="c1">
<div name="div">ccccccccc</div>
<div name="div">ddddddddd</div>
<div name="div">eeeeeeeee</div>
<div class="c1" name="div">fffffffff</div>
</div>
<div class="c1" name="div">gggggggg</div>
<div class="c1" name="div">hhhhhhhh</div>
</body>
$(function () {

    //有时在id或是class中有一些特殊字符如 [等,需要用反斜杠进行转义
$("#div\\#1").html("hello world 1");
$(".c\\[1\\]").html("hello world 2");
//有没有空格的区别
//有空格是选取class等于c1的div里面的name等于div的div(c d e f)
$(".c1 [name=div]").html("hello world 3");
//没有空格是选取class等于c1并且name等于div的div (f g h)
$(".c1[name=div]").html("hello world 4"); });

jQuery常用选择器汇总的更多相关文章

  1. jQuery常用选择器总结

    jQuery常用选择器总结: 我们都知道jQuery是JavaScript(JS)的框架,它的语法简单使用方便,被广大开发人员青睐.现在我就它常用的并且十分强大的选择器的方式,做一个总结.鉴于它的选择 ...

  2. jquery 常用选择器和方法以及遍历(超详细)

    jQuery 常用选择器和和方法 学习总结 一.JQuery 介绍 1. 什么是 jQuery 2. jQuery 版本介绍 3. jQuery 文件介绍 二.jQuery 对象 1. jQuery ...

  3. jquery常用选择器(转)

    jQuery 的选择器可谓之强大无比,这里简单地总结一下常用的元素查找方法 $("#myELement") 选择id值等于myElement的元素,id值不能重复在文档中只能有一个 ...

  4. js和jQuery常用选择器

    笔者觉得js是前台基础中的基础,而其选择器则是js基础中的基础,因长期使用框架导致js生疏,所有查资料,回顾一下js的常用选择器: 1.document.getElementById("id ...

  5. jquery 常用选择器基础语法学习

    siblings方法的常用应用场景:选中高亮 实现代码 <!DOCTYPE html> <html> <head> <meta charset="U ...

  6. jquery常用选择器和常用方法

    基本选择器 $(”#myDiv”) //匹配唯一的具有此id值的元素 $(”div”) //匹配指定名称的所有元素 $(”.myClass”) //匹配具有此class样式值的所有元素 $(”*”) ...

  7. jquery常用选择器

    1.数字性过滤 $("tr:first")               //选择所有tr元素的第一个 $("tr:last")                / ...

  8. jquery及jquery常用选择器使用

    本文为博主原创,未经允许不得转载: 1.jquery强大之处:   容易上手,强大的选择器,解决浏览器的兼容   完善的时间机制,出色的ajax封装,丰富的ui    2.jquery是一个javas ...

  9. jquery 常用选择器 回顾 ajax() parent() parents() children() siblings() find() eq() has() filter() next()

    1. $.ajax() ajax 本身是异步操作,当需要将 异步 改为 同步时: async: false 2.parent()  父级元素  和  parents() 祖先元素 的区别 parent ...

随机推荐

  1. phpMyAdmin中mysql的创建数据库时的编码的问题

    转载自新浪博客    Sean 一. mysql中utf8编码的utf8_bin,utf8_general_cs,utf8_general_ci的区别 utf8_general_ci 不区分大小写,这 ...

  2. linux 安装中文支持包及中文字符集配置

    由于某些原因系统安装时未安装中文支持,导致后续应用出现中文方块乱码现象,解决方法很简单,当然不是重装,只需以下三步即可搞定. 1.安装中文包: #yum -y groupinstall chinese ...

  3. c# List集合的Find方法适用

    CabinEntity为你定义的实体 CabinEntity existCabin = listCabins.Find(delegate(CabinEntity cabinE)             ...

  4. 原 iOS面试题收集

    原 iOS面试题收集 发表于2年前(2013-07-22 13:47)   阅读(369) | 评论(0) 4人收藏此文章, 我要收藏 赞0 听云性能监测产品App.Server.CDN免费试用,绑定 ...

  5. ps存jpeg,格式保存的时候为什么选择“基线”

    jpeg是印前和网页设计常用的格式,最大好处就是能很大程度上压缩图像大小. 在ps中将图片保存为jpeg格式的时候会出现以下选项: 其中:图像选项都很熟悉,是关于图像质量的:而格式选项的用途主要是针对 ...

  6. Codeforces 494D Upgrading Array

    http://codeforces.com/contest/494/problem/D 题意:给一个数组,和一个坏质数集合,可以无数次地让1到i这些所有数字除以他们的gcd,然后要求Σf(a[i])的 ...

  7. nm和readelf命令的区别

    其实问题的本质是对elf格式的理解问题,因为是查看so库的符号表发现的问题. 事情起因是这样的,由于我的一个程序编译的时候出现了undefined reference to “XXX”的错误,需要链接 ...

  8. 关闭ssh密码登录

    我们经常使用SSH登录管理服务器,在享受便利的同时也会遇到一些安全隐患,最常见的就是SSH密码被暴力破解.如果我们将SSH密码设置的过于复杂,又不容易记,因此限制SSH登录方式,只能使用key登录是一 ...

  9. hadoop 1.2.1 安装步骤 伪分布式

    最近在系统的学习hadoop 课程第一步是安装hadoop1.x,具体安装步骤如下: 一.系统安装 本文使用centos6.5安装,具体安装步骤省略 二.jdk安装 下载jdk1.7.0_51解压,在 ...

  10. if switch练习(体重)

    public class shencai { public static void main(String[] args) { int h= 186,g= 80; String Sex = " ...