Jquery 选择器 详解

 

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

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

一、基本选择器

1 $("#div1").html("hello world 1"); //根据id匹配元素(a)
2 $(".c1").html("hello  world 2"); //根据Yclass匹配元素(b)
3 $("span").html("hello world 3"); //根据元素名称匹配 (c)
4 $("span,div.c1").html("hello world 4"); //匹配页面所有的span 和class=c1的div(b c)
5 $("*").html("hello world 5"); //匹配页面所有元素,也包含body

二、层级选择器

1 $("body span").html("hello world 1"); //选取body中所有的span(a b d)
2 $("body>span").html("hello world 2"); //选取body元素的子span元素(a b)
3 $("span.c1+div").html("hello world 3"); //选取class为c1的span的下一个div元素,注意是同级元素
4 $("span.c1").next().html("hello world 3"); //跟上行效果相同 (c)
5 $("span.c1~div").html("hello world 4"); //选取class为c1的span的后面的所有div
6 $("span.c1").nextAll().html("hello world 4"); //跟上行效果相同(c e)

三、基本过滤选择器

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

四、内容过滤选择器

1 $("span:contains(aa)").html("hello world 1"); //选取内容包含aa的span元素
2 $("span:empty").html("hello world 2"); //选取空的span元素
3 $("div:has(span)").html("hello world 3"); //选取包含span的div元素
4 $("span:parent").html("hello world 4"); //选取包含子元素的span元素,包含文本

五、属性过滤选择器

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

六、子元素过滤选择器

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

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

1 $("#form1 input:enabled").val("hello world 1"); //选取form表单中没有禁用的文本框
2     $("#form1 input:disabled").val("hello world 2"); //选取form表单中没有禁用的文本框
3     $("#form1 input:checked").attr("checked",false); //选取form表单中选的复选框
4     $("select option[selected]").each(function () {  //选取 下拉框 选中的 option
5         alert($(this).val());
6     });

八、选择器要注意的地方

 1 <body>
 2     <div id="div#1">aaaaaaaaaaa</div>
 3     <div class="c[1]">bbbbbbbbb</div>
 4     <div class="c1">
 5         <div name="div">ccccccccc</div>
 6         <div name="div">ddddddddd</div>
 7         <div name="div">eeeeeeeee</div>
 8         <div class="c1" name="div">fffffffff</div>
 9     </div>
10     <div class="c1" name="div">gggggggg</div>
11     <div class="c1" name="div">hhhhhhhh</div>
12 </body>
13
14 $(function () {
15     //有时在id或是class中有一些特殊字符如 [ 等,需要用反斜杠进行转义
16     $("#div\\#1").html("hello world 1");
17     $(".c\\[1\\]").html("hello world 2");
18     //有没有空格的区别
19     //有空格是选取class等于c1的div里面的name等于div的div(c d e f)
20     $(".c1 [name=div]").html("hello world 3");
21     //没有空格是选取class等于c1并且name等于div的div (f g h)
22     $(".c1[name=div]").html("hello world 4");
23 });

js 判断字符串是否包含另外一个字符串

 

示例代码:

1     <script type="text/javascript">
2         var str = "测试一个字符串(ehtrzes)是否包含另外一个字符串";
3         if (str.indexOf("(ehtrzes)") >= 0) {
4             alert('字符串中包含(ehtrzes)字符串');
5         } else {
6             alert('字符串中 不包含(ehtrzes)字符串');
7         }
8     </script>

indexOf用法详解:
        返回 String 对象内第一次出现子字符串的字符位置。 
        JavaScript的indexOf忽略大小写
           str.indexOf(subString, startIndex) 
           参数
           strObj 
           必选项。String 对象或文字。 
           subString 
           必选项。要在 String 对象中查找的子字符串。 
           starIndex 
           可选项。该整数值指出在 String 对象内开始查找的索引。如果省略,则从字符串的开始处查找。 
           说明
           indexOf 方法返回一个整数值,指出 String 对象内子字符串的开始位置。如果没有找到子字符串,则返回 -1。 
           如果 startindex 是负数,则 startindex 被当作零。如果它比最大的字符位置索引还大,则它被当作最大的可能索引。 
        indexOf函数方法返回一个整数值,指出 String 对象内子字符串的开始位置。如果没有找到子字符串,则返回 -1。如果 startindex 是负数,则 startindex 被当作零。如果它比最大的字符位置索引还大,则它被当作最大的可能索引。

对于JavaScript的indexOf忽略大小写
        JavaScript中indexOf函数方法返回一个整数值,指出 String 对象内子字符串的开始位置。如果没有找到子字符串,则返回 -1。如果 startindex 是负数,则 startindex 被当作零。如果它比最大的字符位置索引还大,则它被当作最大的可能索引。
        indexOf函数是从左向右执行查找。否则,该方法与 lastIndexOf 相同。

Jquery 选择器 详解 js 判断字符串是否包含另外一个字符串的更多相关文章

  1. js 判断字符串是否包含另外一个字符串

    示例代码: <script type="text/javascript"> var str = "测试一个字符串(ehtrzes)是否包含另外一个字符串&qu ...

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

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

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

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

  4. jQuery选择器详解

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

  5. JS中判断某个字符串是否包含另一个字符串的五种方法

    String对象的方法 方法一: indexOf()   (推荐) ? 1 2 var str = "123" console.log(str.indexOf("2&qu ...

  6. 关于前端JS判断字符串是否包含另外一个字符串的方法总结

    RegExp 对象方法 test() var str = "abcd"; var reg = RegExp(/d/); console.log(reg.test(str)); // ...

  7. js判断某个字符串是否包含另一个字符串

    1.indexOf():推荐,可返回某个指定的字符串值在字符串中首次出现的位置.如果要检索的字符串值没有出现,则该方法返回 -1. var str = "123" console. ...

  8. java中如何判断一个字符串是否包含另外一个字符串的方法

    indexOf(String s)的使用,如果包含,返回的值是包含该子字符串在父类字符串中起始位置: 如果不包含必定全部返回值为-1 package my_automation; public cla ...

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

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

随机推荐

  1. oracle SGA

    3.2 SGA系统全局区--System global area(SGA) 是一组共享的内存结构,它里面存储了oracle数据库实例(instance)的数据和控制文件信息.如果有多个用户同时连接到数 ...

  2. linux install nodejs

    下载/安装python yum install -y bzip2* #nodejs 0.8.5需要,请安装python前,先安装此模块.   wget http://www.python.org/ft ...

  3. eclipse,myeclipse综合

    1.Myeclipse点击发布无反应 进入workspace目录,删除.metadata\.plugins\org.eclipse.core.runtime\.settings\com.genuite ...

  4. 老周发布 UWP 应用的隐私策略(通用)

    UWP 应用隐私策略 前注  本声明通用于老周所发布的所有 UWP 应用,下文简称“应用”.开发者全称:周家安,下文简称“老周”. 1.免责声明 您在使用应用过程中,请遵守<中华人民共和国宪法& ...

  5. 使用PyInstaller打包Python角本为exe程序

    一.经过测试 在Windows平台请使用Windows平台的pyinstaller,Linux平台请使用Linux平台的Pyinstall角本. 二.命令如下: pyinstaller -F --ic ...

  6. hibernate调用mysql存储过程

    在mysql中创建两个存储过程,如下: 1.根据id查找某条数据: )) begin select * from emp where empId=id; end; 2.根据id查找某个字段,并返回 ) ...

  7. 微信小程序字符串如何转数字?

      [微信小程序]字符串如何转数字 字符串可以采用 parseInt()方法来转换为数字. input_number_sim = parseInt(input_number_sim)   也可采用的用 ...

  8. J2EE框架知识清单

    1:Struts MVC.JVC 2:struts action 3:struts 1.0和2.0区别 4:Spring 核心机制:依赖注入 5:使用Spring容器 6:AOP的概念与应用 7:IO ...

  9. 【Thinking in Java】读书笔记

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6480258.html 第三章:操作符 1:基本数据类型的比较用 ==.!=,引用类型的==.!=是针对地址的比 ...

  10. 妙用Pixel bender执行复杂运算/普通数据运算 传递Vector数组

    最近发现pixel bender有两个特殊点: 1.Input Image4,不单单可以用BitmapData来初始化,也可以用Vector.<Number>初始化. 2.ShaderJo ...