在线文档地址:http://tool.oschina.net/apidocs/apidoc?api=jquery

各种在线工具地址:http://www.ostools.net/

一、基本选择器

 $("#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").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)

三、基本过滤选择器

 $("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...

四、内容过滤选择器

 $("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元素,包含文本

五、属性过滤选择器

 $("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元素

六、子元素过滤选择器

 $("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只有一个子元素的子元素

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

 $("#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 () { //选取 下拉框 选中的 option
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 选择器 详解 js 判断字符串是否包含另外一个字符串

    Jquery 选择器 详解   在线文档地址:http://tool.oschina.net/apidocs/apidoc?api=jquery 各种在线工具地址:http://www.ostools ...

  2. jQuery选择器详解

    根据所获取页面中元素的不同.可以将jQuery选择器分为:四大类,其中过滤选择器在分为六小类 jQuery选择器 基本选择器   层次选择器   过滤选择器 简单过滤选择器 内容过滤选择器 可见性过滤 ...

  3. jQuery选择器详解及实例---《转载》

    选择器是jQuery最基础的东西,本文中列举的选择器基本上囊括了所有的jQuery选择器,也许各位通过这篇文章能够加深对jQuery选择器的理解,它们本身用法就非常简单,我更希望的是它能够提升个人编写 ...

  4. jQuery-强大的jQuery选择器 (详解)[转]

      1. 基础选择器 Basics 名称 说明 举例 #id 根据元素Id选择 $("divId") 选择ID为divId的元素 element 根据元素的名称选择, $(&quo ...

  5. [置顶] Jquery学习总结(二) jquery选择器详解

    1.基本选择器 l ID 根据元素ID选择 l Elementname 根据元素名称选择 l Classname 根据元素css类名选择 举例: <input type=”text” id=”I ...

  6. jQuery选择器 (详解)

    1. 基础选择器 Basics 名称 说明 举例 #id 根据元素Id选择 $("divId") 选择ID为divId的元素 element 根据元素的名称选择, $(" ...

  7. [转]jQuery选择器 (详解)

    1).基本 #id 根据给定的ID匹配一个元素.例如:$("#id")element 根据给定的元素名匹配所有元素.例如:$("div").class 根据给定 ...

  8. [JQuery]选择器详解

      示例 说明 $(this) 当前元素 $("p") 所有<p>元素 $("input") 所有input元素 $(".intro&qu ...

  9. jQuery.validator 详解二

    前言:上一篇详细的介绍了jQuery.validator( 版本v1.13.0 )的验证规则,这一篇重点讲述它的源码结构,及如何来对元素进行验证,错误消息提示的内部实现 一.插件结构(组织方式) 在讲 ...

随机推荐

  1. Selenium IDE和Selenium RC的安装

    1       安装FireBug和FirePath 1.在火狐浏览器中,点击”添加附件”按钮,弹出”附加组件管理器”页面 2.在弹出页面中,输入“fireBug”,点击“搜索”按钮,弹出fireBu ...

  2. 在虚拟机上安装Linux6.5

    下定决心开始学习Linux了,这个博客将记录我的成长点滴,方便日后温故而知新!并希望有小伙伴能给出意见和建议! 我用的是VMware Workstation 10,当然是破解版,毕竟只是自己做练习使用 ...

  3. 如何防止JAVA反射对单例类的攻击?

    在我的上篇随笔中,我们知道了创建单例类有以下几种方式: (1).饿汉式; (2).懒汉式(.加同步锁的懒汉式.加双重校验锁的懒汉式.防止指令重排优化的懒汉式); (3).登记式单例模式; (4).静态 ...

  4. Visual Studio C# IntelliSense not automatically displaying

    Options -> Text Editor -> C# -> IntelliSense

  5. modal的使用

    $modal是一个可以迅速创建模态窗口的服务,创建部分页,控制器,并关联他们 $modal仅有一个方法open(options) templateUrl:模态窗口的地址 template:用于显示ht ...

  6. css居中解决方案

    水平居中 行内或者具有行内元素性质的元素(比如文字或者链接)? 让一个父元素为块级元素的行内元素水平居中,可以:CSS: 1 2 3 .center-children { text-align: ce ...

  7. [XAF] How to represent an enumeration property via a drop-down box with check boxes

    https://www.devexpress.com/Support/Center/Example/Details/E689

  8. 使用netty实现的tcp通讯中如何实现同步返回

    在netty实现的tcp通讯中,一切都是异步操作,这提高了系统性能,但是,有时候client需要同步等待消息返回,如何实现呢?笔者已经实现,在此总结下重点要素 实现要点: 1.消息结构设计 消息头中需 ...

  9. *关于httl开源Java模板的使用心得

    1.简介 HTTL (Hyper-Text Template Language) 是一个高性能的开源JAVA模板引擎, 适用于动态HTML页面输出, 可替代JSP页面, 指令和Velocity相似. ...

  10. libmegjb.so加载问题调试和分析

    欢迎转载opendevkit文章, 文章原始地址: http://www.opendevkit.com/?e=70 01-01 00:09:21.084: D/dalvikvm(10394): Try ...