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. idea 提交代码时提示 please tell me who you are .......

  2. [Functional Programming] Arrow Functor with contramap

    What is Arrow Functor? Arrow is a Profunctor that lifts a function of type a -> b and allows for ...

  3. 关于 URL 编码及 JavaScript 编码函数【转载+整理】

    原文地址:http://www.ruanyifeng.com/blog/2010/02/url_encoding.html 本文内容 引入 环境 测试 JavaScript 编码函数   引入 URL ...

  4. linux ifconfig -a

    export PATH=$PATH:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/bin:/usr/i686-pc ...

  5. 使用Filter过滤非法内容

    1.首先,需要编写一个响应的封装器ResponseReplaceWrapper,用它来缓存response中的内容,代码如下: ResponseReplaceWrapper.java package ...

  6. javascript中this的妙用

    this是javascript语言的一个关键字,它代表函数运行时,自动生成的一个内部对象,只能在函数内部使用. this总是指向对象,并且为调用函数的那个对象: //调用普通函数 function f ...

  7. 【推荐】ImageProcessor.Web,再也不用自己生成缩略图了

    1.什么是ImageProcessor.Web ImageProcessor.Web是基于ImageProcessor的web图像处理模块,允许开发者使用URL查询字符串参数的方式作为指令执行图像处理 ...

  8. ajax 二级联动与springmvc 交互

    html  测试可以使用 <div class="pageButton" style="height: 60px;margin: 10px;line-height: ...

  9. markdown table语法

    普通的表格 | 一个普通标题 | 一个普通标题 | 一个普通标题 | | ------ | ------ | ------ | | 短文本 | 中等文本 | 稍微长一点的文本 | | 稍微长一点的文本 ...

  10. 轻松把玩HttpClient之封装HttpClient工具类(五),携带Cookie的请求

    近期更新了一下HttpClientUtil工具类代码,主要是加入了一个參数HttpContext,这个是用来干嘛的呢?事实上是用来保存和传递Cookie所须要的. 由于我们有非常多时候都须要登录.然后 ...