Bootstrap【Jquery序列】之Jquery 采用器

1   概述

 本篇作品为穿插随笔,ASP.NET
MVC序列如今写了如下几篇:

写该篇著作重要目标是为接下去的ASP.NET MVC
体系,如页面之间传值,ADO.NET和EF,Bootstrap等打基础,本篇随笔相比较基础,但相比全面,万分适合基础迅速扫盲。

2   基本接纳器

2.1  一览表

Bootstrap 1

2.2  示例代码

 (1)id选择器

将id为lastname元素的背景观设置为黄色

Bootstrap 2Bootstrap 3

 1 <!DOCTYPE html>
 2 
 3 <html>
 4 <head>
 5     <meta name="viewport" content="width=device-width" />
 6     <script src="~/Scripts/jquery-1.10.2.js"></script>
 7     <title>JQuery函数</title>
 8     <script type="text/javascript">
 9         $(document).ready(function () {
10             $("#lastname").css("background-color", "#0000ff");
11            
12         });
13 
14     </script>
15 </head>
16 <body>
17     <div id="lastname">id为lastname的选择器</div>
18 </body>
19 </html>

View
Code

(2)类选取器

将class为intro元素的背景色设置为藏蓝色

Bootstrap 4Bootstrap 5

 1 <!DOCTYPE html>
 2 
 3 <html>
 4 <head>
 5     <meta name="viewport" content="width=device-width" />
 6     <script src="~/Scripts/jquery-1.10.2.js"></script>
 7     <title>JQuery函数</title>
 8     <script type="text/javascript">
 9         $(document).ready(function () {
10             $(".intro").css("background-color", "#0000ff");
11            
12         });
13 
14     </script>
15 </head>
16 <body>
17     <div class="intro">div选择器测试</div>
18     <p class="intro">p测试选择器</p>
19 </body>
20 </html>

View
Code

(3)元素采用器

将p元素的背景观设置为灰色

Bootstrap 6Bootstrap 7

 1 <!DOCTYPE html>
 2 
 3 <html>
 4 <head>
 5     <meta name="viewport" content="width=device-width" />
 6     <script src="~/Scripts/jquery-1.10.2.js"></script>
 7     <title>JQuery函数</title>
 8     <script type="text/javascript">
 9         $(document).ready(function () {
10             $("p").css("background-color", "#0000ff");
11            
12         });
13 
14     </script>
15 </head>
16 <body>
17      <p>p测试选择器</p>
18 </body>
19 </html>

View
Code

(4)所有采取器

遍历body下的有着因素,将其背景观设置为肉色

Bootstrap 8Bootstrap 9

 1 <!DOCTYPE html>
 2 
 3 <html>
 4 <head>
 5     <meta name="viewport" content="width=device-width" />
 6     <script src="~/Scripts/jquery-1.10.2.js"></script>
 7     <title>JQuery函数</title>
 8     <script type="text/javascript">
 9         $(document).ready(function () {
10             $("body *").css("background-color", "#0000ff");
11            
12         });
13 
14     </script>
15 </head>
16 <body>
17     <div>选择器测试</div>
18     <p>p元素</p>
19 </body>
20 </html>

View
Code

(5)并列接纳器

 将元素p和因素div背景观设置为粉色

Bootstrap 10Bootstrap 11

 1 <!DOCTYPE html>
 2 
 3 <html>
 4 <head>
 5     <meta name="viewport" content="width=device-width" />
 6     <script src="~/Scripts/jquery-1.10.2.js"></script>
 7     <title>JQuery函数</title>
 8     <script type="text/javascript">
 9         $(document).ready(function () {
10             $("p,div").css("background-color", "#0000ff");
11            
12         });
13 
14     </script>
15 </head>
16 <body>
17     <div>选择器测试</div>
18     <p>p元素</p>
19 </body>
20 </html>

View
Code

3   层次接纳器

3.1 一览表

Bootstrap 12

3.2 示例代码

(1)parent>child(直系子元素,即直接下一代元素)

设置div元素的首先代元素为span的因素的背景象为绿色

Bootstrap 13Bootstrap 14

 1 <!DOCTYPE html>
 2 
 3 <html>
 4 <head>
 5     <meta name="viewport" content="width=device-width" />
 6     <script src="~/Scripts/jquery-1.10.2.js"></script>
 7     <title>JQuery函数</title>
 8     <script type="text/javascript">
 9         $(document).ready(function () {
10             $("div>span").css("background-color", "#0000ff");
11            
12         });
13 
14     </script>
15 </head>
16 <body>
17     <div>
18         DOM树,DIV第一代
19         <p>
20             DOM树,第二代
21         </p>
22         DOM树,DIV第一代
23     </div>
24 </body>
25 </html>

View
Code

测试结果:

Bootstrap 15

结果分析:依据如上代码画出的DOM树如下,可以很清晰看到,DIV有五个一向孩子,即首先代span,p,span,代码中div>span,表示div下的第一手第一代span,由此,测试结果就不难领悟了。

Bootstrap 16

(2)prev+next(prev元素的下一个弟兄元素,等同于next()方法)

设置类为intro元素的下一个兄弟元素背景象为粉色

Bootstrap 17Bootstrap 18

 1  <!DOCTYPE html>
 2 
 3 <html>
 4 <head>
 5     <meta name="viewport" content="width=device-width" />
 6     <script src="~/Scripts/jquery-1.10.2.js"></script>
 7     <title>JQuery函数</title>
 8     <script type="text/javascript">
 9         $(document).ready(function () {
10             $(".intro+div").css("background-color", "#0000ff");
11             //$(".intro").next("div").css("background-color", "#0000ff");
12            
13         });
14 
15     </script>
16 </head>
17 <body>
18     <div>1</div>
19     <p class="intro">2</p>
20     <div>3</div>
21     <div>4</div>
22     5
23     <div>6</div>
24 </body>
25 </html>

View
Code

测试结果:

Bootstrap 19

结果分析:依照如上代码画出DOM树如下图,测试结果肯定。

Bootstrap 20

(3)prev~siblings(prev元素的具备兄弟元素,等同于nextAll()方法)

 设置类为intro元素之后的有着兄弟元素为div元素的背景观为灰色

Bootstrap 21Bootstrap 22

 1 <!DOCTYPE html>
 2 
 3 <html>
 4 <head>
 5     <meta name="viewport" content="width=device-width" />
 6     <script src="~/Scripts/jquery-1.10.2.js"></script>
 7     <title>JQuery函数</title>
 8     <script type="text/javascript">
 9         $(document).ready(function () {
10             $(".intro~div").css("background-color", "#0000ff");
11         });
12 
13     </script>
14 </head>
15 <body>
16     <div>G0</div>
17     <div class="intro">G1</div>
18     <div>G2</div>
19     G3
20     <div>G4</div>
21 </body>
22 </html>

View
Code

测试结果:

Bootstrap 23

分析测试结果:遵照如上代码画出DOM树如下图,测试结果肯定。

Bootstrap 24

4   过滤选用器

4.1 基本过滤采取器

4.1.1  一览表

Bootstrap 25

4.1.2  代码示例

(1):first(选用第一个因素)

Bootstrap 26Bootstrap 27

 1 <!DOCTYPE html>
 2 
 3 <html>
 4 <head>
 5     <meta name="viewport" content="width=device-width" />
 6     <script src="~/Scripts/jquery-1.10.2.js"></script>
 7     <title>JQuery函数</title>
 8     <script type="text/javascript">
 9         $(document).ready(function () {
10             $("span:first").css("background-color", "#0000ff");
11         });
12 
13     </script>
14 </head>
15 <body>
16     G1
17     G2
18     G3
19 </body>
20 </html>

View
Code

测试结果:

Bootstrap 28

(2):last(选拔最终一个因素)

Bootstrap 29Bootstrap 30

 1 <!DOCTYPE html>
 2 
 3 <html>
 4 <head>
 5     <meta name="viewport" content="width=device-width" />
 6     <script src="~/Scripts/jquery-1.10.2.js"></script>
 7     <title>JQuery函数</title>
 8     <script type="text/javascript">
 9         $(document).ready(function () {
10             $("span:last").css("background-color", "#0000ff");
11         });
12 
13     </script>
14 </head>
15 <body>
16     G1
17     G2
18     G3
19 </body>
20 </html>

View
Code

测试结果:

Bootstrap 31

(3):not(取非元素)

Bootstrap 32Bootstrap 33

 1 <!DOCTYPE html>
 2 
 3 <html>
 4 <head>
 5     <meta name="viewport" content="width=device-width" />
 6     <script src="~/Scripts/jquery-1.10.2.js"></script>
 7     <title>JQuery函数</title>
 8     <script type="text/javascript">
 9         $(document).ready(function () {
10             $("div:not(.wrap)").css("background-color", "#0000ff");
11         });
12 
13     </script>
14 </head>
15 <body>
16     <div>G1</div>
17     <div class="wrap">G2</div>
18 </body>
19 </html>

View
Code

只是,请小心下边的代码:当G1所在div和G2所在div是父子关系时,G1和G2都会变色。

Bootstrap 34Bootstrap 35

1 <div>
2     G1    <div class="wrap">G2</div>
3 </div>

View
Code

(4):even(索引为偶数,索引
index从0开始)

Bootstrap 36Bootstrap 37

 1 <!DOCTYPE html>
 2 
 3 <html>
 4 <head>
 5     <meta name="viewport" content="width=device-width" />
 6     <script src="~/Scripts/jquery-1.10.2.js"></script>
 7     <title>JQuery函数</title>
 8     <script type="text/javascript">
 9         $(document).ready(function () {
10             $("div:even").css("background-color", "#0000ff");
11         });
12 
13     </script>
14 </head>
15 <body>
16     <div>G1</div>
17     <div>G2</div>
18     <div>G3</div>
19     <div>G4</div>
20 </body>
21 </html>

View
Code

测试结果:

Bootstrap 38

(5):odd(索引为奇数,索引
index从0起始)

Bootstrap 39Bootstrap 40

 1 <!DOCTYPE html>
 2 
 3 <html>
 4 <head>
 5     <meta name="viewport" content="width=device-width" />
 6     <script src="~/Scripts/jquery-1.10.2.js"></script>
 7     <title>JQuery函数</title>
 8     <script type="text/javascript">
 9         $(document).ready(function () {
10             $("div:odd").css("background-color", "#0000ff");
11         });
12 
13     </script>
14 </head>
15 <body>
16     <div>G1</div>
17     <div>G2</div>
18     <div>G3</div>
19     <div>G4</div>
20 </body>
21 </html>

View
Code

测试结果:

Bootstrap 41

(6):eq(x)(取指定索引的元素,x为从0开头的目录)

安装索引为2的div元素背景为灰色

Bootstrap 42Bootstrap 43

 1 <!DOCTYPE html>
 2 
 3 <html>
 4 <head>
 5     <meta name="viewport" content="width=device-width" />
 6     <script src="~/Scripts/jquery-1.10.2.js"></script>
 7     <title>JQuery函数</title>
 8     <script type="text/javascript">
 9         $(document).ready(function () {
10             $("div:eq(2)").css("background-color", "#0000ff");
11         });
12 
13     </script>
14 </head>
15 <body>
16     <div>G1</div>
17     <div>G2</div>
18     <div>G3</div>
19     <div>G4</div>
20 </body>
21 </html>

View
Code

测试结果:

Bootstrap 44

(7):lt(x))(取小于指定索引的因素,x为从0起初的目录)

Bootstrap 45Bootstrap 46

 1 <!DOCTYPE html>
 2 
 3 <html>
 4 <head>
 5     <meta name="viewport" content="width=device-width" />
 6     <script src="~/Scripts/jquery-1.10.2.js"></script>
 7     <title>JQuery函数</title>
 8     <script type="text/javascript">
 9         $(document).ready(function () {
10             $("div:lt(2)").css("background-color", "#0000ff");
11         });
12 
13     </script>
14 </head>
15 <body>
16     <div>G1</div>
17     <div>G2</div>
18     <div>G3</div>
19     <div>G4</div>
20 </body>
21 </html>

View
Code

测试结果:

Bootstrap 47

(8):gt(x))(取大于指定索引的元素,x为从0先导的目录)

Bootstrap 48Bootstrap 49

 1 <!DOCTYPE html>
 2 
 3 <html>
 4 <head>
 5     <meta name="viewport" content="width=device-width" />
 6     <script src="~/Scripts/jquery-1.10.2.js"></script>
 7     <title>JQuery函数</title>
 8     <script type="text/javascript">
 9         $(document).ready(function () {
10             $("div:gt(2)").css("background-color", "#0000ff");
11         });
12 
13     </script>
14 </head>
15 <body>
16     <div>G1</div>
17     <div>G2</div>
18     <div>G3</div>
19     <div>G4</div>
20 </body>
21 </html>

View
Code

测试结果:

Bootstrap 50

(8):header(取h1-h6标题元素)

Bootstrap 51Bootstrap 52

 1 <!DOCTYPE html>
 2 
 3 <html>
 4 <head>
 5     <meta name="viewport" content="width=device-width" />
 6     <script src="~/Scripts/jquery-1.10.2.js"></script>
 7     <title>JQuery函数</title>
 8     <script type="text/javascript">
 9         $(document).ready(function () {
10             $(":header").css("background-color", "#0000ff");
11         });
12 
13     </script>
14 </head>
15 <body>
16     <h1>测试H1标题</h1>
17     <div>G1</div>
18     <div>G2</div>
19     <div>G3</div>
20     <h2>测试h2标题</h2>
21     <h3>测试h3标题</h3>
22     <h4>测试h4标题</h4>
23     <div>G4</div>
24     <h5>测试h5标题</h5>
25     <h6>测试h6标题</h6>
26 </body>
27 </html>

View
Code

测试结果:

Bootstrap 53

(9):animated(所有动画元素)

Bootstrap 54Bootstrap 55

 1 <!DOCTYPE html>
 2 
 3 <html>
 4 <head>
 5     <meta name="viewport" content="width=device-width" />
 6     <script src="~/Scripts/jquery-1.10.2.js"></script>
 7     <title>JQuery函数</title>
 8     <script type="text/javascript">
 9          $(document).ready(function () {
10             function aniDiv() {
11                 $("#box").animate({ width: 300 }, "slow");
12                 $("#box").animate({ width: 100 }, "slow", aniDiv);
13             }
14             aniDiv();
15             $(".btn1").click(function () {
16                 $(":animated").css("background-color", "#0000ff");
17             });
18         });
19 
20     </script>
21     <style>
22         div {
23                 background: #98bf21;
24                 height: 40px;
25                 width: 100px;
26                 position: relative;
27                 margin-bottom: 5px;
28              }
29     </style>
30 </head>
31 <body>
32     <div></div>
33     <div id="box"></div>
34     <div></div>
35     <button class="btn1">Mark animated element</button>
36 </body>
37 </html>

View
Code

测试结果:

 Bootstrap 56

4.2 内容过滤选拔器

4.2.1 一览表

Bootstrap 57

4.2.2 示例代码

(1):contains(text)(取包含text文本的要素)

Bootstrap 58Bootstrap 59

 1 <!DOCTYPE html>
 2 
 3 <html>
 4 <head>
 5     <meta name="viewport" content="width=device-width" />
 6     <script src="~/Scripts/jquery-1.10.2.js"></script>
 7     <title>JQuery函数</title>
 8     <script type="text/javascript">
 9          $(document).ready(function () {
10              $('div:contains("G2")').css("background-color", "#0000ff");
11          });
12 
13     </script>
14     
15 </head>
16 <body>
17     <div>G1</div>
18     <div>G2</div>
19     <div>G3</div>
20 </body>
21 </html>

View
Code

测试结果:

Bootstrap 60

(2):empty(取不带有子元素或文本为空的元素)

Bootstrap 61Bootstrap 62

 1 <!DOCTYPE html>
 2 
 3 <html>
 4 <head>
 5     <meta name="viewport" content="width=device-width" />
 6     <script src="~/Scripts/jquery-1.10.2.js"></script>
 7     <title>JQuery函数</title>
 8     <script type="text/javascript">
 9          $(document).ready(function () {
10              $('div:empty').html('没有内容');
11          });
12 
13     </script>
14     
15 </head>
16 <body>
17     <div>G1</div>
18     <div>G2</div>
19     <div>G3</div>
20     <div></div>
21 </body>
22 </html>

View
Code

测试结果:

Bootstrap 63

(3) :has(selector)(取拔取器匹配的因素)

即便span不是div的直系子元素,也会立竿见影

Bootstrap 64Bootstrap 65

 1 <!DOCTYPE html>
 2 
 3 <html>
 4 <head>
 5     <meta name="viewport" content="width=device-width" />
 6     <script src="~/Scripts/jquery-1.10.2.js"></script>
 7     <title>JQuery函数</title>
 8     <script type="text/javascript">
 9         $(document).ready(function () {
10             // 为包含span元素的div添加边框
11             $('div:has(span)').css('border', '1px solid #000');
12         });
13 
14     </script>
15     
16 </head>
17 <body>
18     <div>
19         <h2>
20             A     B
21         </h2>
22     </div>
23 </body>
24 </html>

View
Code

测试结果:

Bootstrap 66

(4):parent(取包含子元素或文本的元素)

Bootstrap 67Bootstrap 68

 1 <!DOCTYPE html>
 2 
 3 <html>
 4 <head>
 5     <meta name="viewport" content="width=device-width" />
 6     <script src="~/Scripts/jquery-1.10.2.js"></script>
 7     <title>JQuery函数</title>
 8     <script type="text/javascript">
 9         $(document).ready(function () {
10             $('ol li:parent').css('border', '1px solid #000');
11         });
12 
13     </script>
14     
15 </head>
16 <body>
17     <ol>
18         <li></li>
19         <li>A</li>
20         <li></li>
21         <li>D</li>
22     </ol>
23 </body>
24 </html>

View
Code

测试结果:

Bootstrap 69

4.3 可见性过滤采取器

4.3.1 一览表

Bootstrap 70

4.3.2 示例代码

(1):hidden(取不可见的要素)

匹配display:none,<input type=”hidden”
/>,visibility:hidden,capacity:0元素

Bootstrap 71Bootstrap 72

 1 <!DOCTYPE html>
 2 
 3 <html>
 4 <head>
 5     <meta name="viewport" content="width=device-width" />
 6     <script src="~/Scripts/jquery-1.10.2.js"></script>
 7     <title>JQuery函数</title>
 8     <script type="text/javascript">
 9         $(document).ready(function () {
10             $('div:hidden').show(500);
11             alert($('input:hidden').val());
12         });
13     </script>
14     <style type="text/css">
15             div         
16             {
17                 margin: 10px;
18                 width: 200px;
19                 height: 40px;
20                 border: 1px solid #FF0000;
21                 display:block;
22              }
23 
24             .hid-1         
25             {
26                 display: none;
27              }
28 
29             .hid-2         
30             {
31                 visibility: hidden;
32             }
33         
34     </style>
35 </head>
36 <body>
37     <div class="hid-1">display: none</div>
38     <div class="hid-2">visibility: hidden</div>
39     <input type="hidden" value="hello" />
40 </body>
41 </html>

View
Code

测试结果:

Bootstrap 73

(2):visible(取可见的因素)

Bootstrap 74Bootstrap 75

 1 <!DOCTYPE html>
 2 
 3 <html>
 4 <head>
 5     <meta name="viewport" content="width=device-width" />
 6     <script src="~/Scripts/jquery-1.10.2.js"></script>
 7     <title>JQuery函数</title>
 8     <script type="text/javascript">
 9          $(document).ready(function () {
10              $('div:visible').css("background-color", "#0000ff");
11          });
12 
13     </script>
14     
15 </head>
16 <body>
17     <div style="display:none">G1</div>
18     <div>G2</div>
19     <div style="display:none">G3</div>
20     <div>G4</div>
21 
22 </body>
23 </html>
24 
25 
26   

View
Code

测试结果:

Bootstrap 76

4.4 属性过滤采纳器

4.4.1 一览表

Bootstrap 77

4.4.2 代码示例

(1)[attribute](取拥有attribute属性的元素)

Bootstrap 78Bootstrap 79

 1 <!DOCTYPE html>
 2 
 3 <html>
 4 <head>
 5     <meta name="viewport" content="width=device-width" />
 6     <script src="~/Scripts/jquery-1.10.2.js"></script>
 7     <title>JQuery函数</title>
 8     <script type="text/javascript">
 9          $(document).ready(function () {
10              $('div[class]').css("background-color", "#0000ff");
11          });
12 
13     </script>
14     
15 </head>
16 <body>
17     <div>G1</div>
18     <div>G2</div>
19     <div>G3</div>
20     <div class="div4">G4</div>
21 
22 </body>
23 </html>
24 
25 
26   

View
Code

测试结果:

Bootstrap 80

(2)[attribute =
value](取attribute属性值等于value)

Bootstrap 81Bootstrap 82

 1 <!DOCTYPE html>
 2 
 3 <html>
 4 <head>
 5     <meta name="viewport" content="width=device-width" />
 6     <script src="~/Scripts/jquery-1.10.2.js"></script>
 7     <title>JQuery函数</title>
 8     <script type="text/javascript">
 9          $(document).ready(function () {
10              $('div[class=div3]').css("background-color", "#0000ff");
11          });
12 
13     </script>
14     
15 </head>
16 <body>
17     <div>G1</div>
18     <div>G2</div>
19     <div class="div3">G3</div>
20     <div class="div4">G4</div>
21 
22 </body>
23 </html>
24 
25 
26   

View
Code

测试结果:

Bootstrap 83

(3) [attribute !=
value](取attribute属性值不对等value的要素)

Bootstrap 84Bootstrap 85

 1 <!DOCTYPE html>
 2 
 3 <html>
 4 <head>
 5     <meta name="viewport" content="width=device-width" />
 6     <script src="~/Scripts/jquery-1.10.2.js"></script>
 7     <title>JQuery函数</title>
 8     <script type="text/javascript">
 9          $(document).ready(function () {
10              $('div[class!=div3]').css("background-color", "#0000ff");
11          });
12 
13     </script>
14     
15 </head>
16 <body>
17     <div>G1</div>
18     <div>G2</div>
19     <div class="div3">G3</div>
20     <div class="div4">G4</div>
21 
22 </body>
23 </html>
24 
25 
26   

View
Code

测试结果:

Bootstrap 86

(4)[attribute $=
value](attribute属性值以value截至)

Bootstrap 87Bootstrap 88

 1 <!DOCTYPE html>
 2 
 3 <html>
 4 <head>
 5     <meta name="viewport" content="width=device-width" />
 6     <script src="~/Scripts/jquery-1.10.2.js"></script>
 7     <title>JQuery函数</title>
 8     <script type="text/javascript">
 9          $(document).ready(function () {
10              $('[id$=div]').css("background-color", "#0000ff");
11          });
12 
13     </script>
14     
15 </head>
16 <body>
17     <div id="first_div">G1</div>
18     <div id="second_div">G2</div>
19     <div class="div3" title="3div">G3</div>
20     <div class="div4">G4</div>
21 
22 </body>
23 </html>

View
Code

测试结果:

Bootstrap 89

(5))[attribute^=
value](attribute属性值以value起首)

Bootstrap 90Bootstrap 91

 1 <!DOCTYPE html>
 2 
 3 <html>
 4 <head>
 5     <meta name="viewport" content="width=device-width" />
 6     <script src="~/Scripts/jquery-1.10.2.js"></script>
 7     <title>JQuery函数</title>
 8     <script type="text/javascript">
 9          $(document).ready(function () {
10              $('[id^=first]').css("background-color", "#0000ff");
11          });
12 
13     </script>
14     
15 </head>
16 <body>
17     <div id="first_div">G1</div>
18     <div id="second_div">G2</div>
19     <div class="div3" title="3div">G3</div>
20     <div class="div4">G4</div>
21 
22 </body>
23 </html>
24 
25 
26   

View
Code

测试结果:

Bootstrap 92

(6)[attribute *=
value](attribute属性值包含value值)

Bootstrap 93Bootstrap 94

 1 <!DOCTYPE html>
 2 
 3 <html>
 4 <head>
 5     <meta name="viewport" content="width=device-width" />
 6     <script src="~/Scripts/jquery-1.10.2.js"></script>
 7     <title>JQuery函数</title>
 8     <script type="text/javascript">
 9          $(document).ready(function () {
10              $('[id*=first]').css("background-color", "#0000ff");
11          });
12 
13     </script>
14     
15 </head>
16 <body>
17     <div id="first_div">G1</div>
18     <div id="second_div">G2</div>
19     <div class="div3" title="3div">G3</div>
20     <div class="div4">G4</div>
21 
22 </body>
23 </html>

View
Code

测试结果:

Bootstrap 95

表明:在性能采用器中,^$符号和正则表达式的发端终结符号表示的意思是平等的,*张冠李戴匹配,类似于sql中的like
‘%str%’。

(7)[selector1][selector2](复合型属性过滤器,同时满意五个原则)

Bootstrap 96Bootstrap 97

 1 <!DOCTYPE html>
 2 
 3 <html>
 4 <head>
 5     <meta name="viewport" content="width=device-width" />
 6     <script src="~/Scripts/jquery-1.10.2.js"></script>
 7     <title>JQuery函数</title>
 8     <script type="text/javascript">
 9          $(document).ready(function () {
10              $('div[class=div3][title=3div]').css("background-color", "#0000ff");
11          });
12 
13     </script>
14     
15 </head>
16 <body>
17     <div>G1</div>
18     <div>G2</div>
19     <div class="div3" title="3div">G3</div>
20     <div class="div4">G4</div>
21 
22 </body>
23 </html>
24 
25 
26   

View
Code

测试结果:

Bootstrap 98

4.5 子元素过滤拔取器

4.5.1 一览表

Bootstrap 99

4.5.2 代码示例

(1)first-child(表示万分的率先个要素)和last-child(表示异常的末梢一个子元素)

 需要大家瞩目标是,:fisrst和:last再次来到的都是单个元素,而:first-child和:last-child重回的都是集合元素。举个
例子:div:first重回的是所有DOM文档中率先个div元素,而div:first-child是回去所有div元素下的第一个因素合并后的集
合。

Bootstrap 100Bootstrap 101

 1 <!DOCTYPE html>
 2 
 3 <html>
 4 <head>
 5     <meta name="viewport" content="width=device-width" />
 6     <script src="~/Scripts/jquery-1.10.2.js"></script>
 7     <title>JQuery函数</title>
 8     <script type="text/javascript">
 9         $(document).ready(function () {
10             $("div:first-child").css("background-color", "#B2E0FF");
11             $("div:last-child").css("background-color", "red");
12         });
13     </script>
14 </head>
15 <body>
16      <div>
17          <div>1</div>
18          <div>2</div>
19          <p>3</p>
20      </div>
21     <div>4</div>
22     <div>last</div>
23 </body>
24 </html>

View
Code

测试结果:

Bootstrap 102

(3)only-child(当某个元素有且仅有一个子元素时,:only-child才会立竿见影)

Bootstrap 103Bootstrap 104

 1 <!DOCTYPE html>
 2 
 3 <html>
 4 <head>
 5     <meta name="viewport" content="width=device-width" />
 6     <script src="~/Scripts/jquery-1.10.2.js"></script>
 7     <title>JQuery函数</title>
 8     <script type="text/javascript">
 9         $(document).ready(function () {
10             $("div:only-child").css("background-color", "red");
11          });
12     </script>
13 </head>
14 <body>
15      <div>
16          <div>1</div>
17          <div>2</div>
18          <p>3</p>
19      </div>
20     <div>4</div>
21     <div>last
22     <div>ddd</div>
23     </div>
24 </body>
25 </html>

View
Code

测试结果:

Bootstrap 105

(4)nth-child

看到这么些就想起英文单词里的,fourth,
fifth,
sixth……,nth表示第n个,:nth-child就代表第n个child元素。要注意的是,这儿的n不像eq(x)、gt(x)或lt(x)是从
0起首的,它是从1开头的,英文里好像也并未zeroth这样的序号词吗。

:nth-child有二种用法:

1) :nth-child(x),获取第x个子元素

2)
:nth-child(even)和:nth-child(odd),从1方始,获取第偶数个要素或第奇数个要素

3)
:nth-child(xn+y),x>=0,y>=0。例如x = 3, y =
0时就是3n,表示取第3n个要素(n>=0)。实际上xn+y是下边二种的通项式。(当x=0,y>=0时,等同于:hth-
child(x);当x=2,y=0时,等同于nth-child(even);当x=2,y=1时,等同于:nth-child(odd))

4.6 表单对象属性过滤采纳器

4.6.1 一览表

Bootstrap 106

4.6.2 代码示例

(1):enabled和:disabled(取可用或不可用元素)

:enabled和:diabled的十分范围包括input,
select, textarea

Bootstrap 107Bootstrap 108

 1 <!DOCTYPE html>
 2 
 3 <html>
 4 <head>
 5     <meta name="viewport" content="width=device-width" />
 6     <script src="~/Scripts/jquery-1.10.2.js"></script>
 7     <title>JQuery函数</title>
 8     <script type="text/javascript">
 9         $(document).ready(function () {
10             $(':enabled').css('border', '1px solid #FF0000');
11             $(':disabled').css('border', '1px solid #0000FF');
12         });
13 
14     </script>
15     
16 </head>
17 <body>
18     <div>
19         <input type="text" value="可用的文本框" />
20     </div>
21     <div>
22         <input type="text" disabled="disabled" value="不可用的文本框" />
23     </div>
24     <div>
25         <textarea disabled="disabled">不可用的文本域</textarea>
26     </div>
27     <div>
28         <select disabled="disabled">
29             <option>English</option>
30             <option>简体中文</option>
31         </select>
32     </div>
33 </body>
34 </html>

View
Code

测试结果:

Bootstrap 109

(2):checked(取选中的单选框或复选框元素)

Bootstrap 110Bootstrap 111

 1 <!DOCTYPE html>
 2 
 3 <html>
 4 <head>
 5     <meta name="viewport" content="width=device-width" />
 6     <script src="~/Scripts/jquery-1.10.2.js"></script>
 7     <title>JQuery函数</title>
 8     <script type="text/javascript">
 9         $(document).ready(function () {
10             $(".btn1").click(function () {
11                 $(":checked").hide();
12             });
13         });
14     </script>
15     
16 </head>
17 <body>
18     <input type="radio" name="sex" value="male" checked="checked" /> Male
19     <br />
20     <input type="radio" name="sex" value="female" /> Female
21     <br />
22     I have a bike:
23     <input type="checkbox" name="vehicle" value="Bike" />
24     <br />
25     I have a car:
26     <input type="checkbox" name="vehicle" value="Car" checked="checked" />
27     <br />
28     I have an airplane:
29     <input type="checkbox" name="vehicle" value="Airplane" />
30     <button class="btn1">Hide Checked Options</button>
31 
32 </body>
33 </html>

View
Code

(3):selected(取下拉列表被入选的要素)

Bootstrap 112Bootstrap 113

 1 <html>
 2 <head>
 3 <script type="text/javascript" src="/jquery/jquery.js"></script>
 4 <script type="text/javascript">
 5 $(document).ready(function(){
 6   $(".btn1").click(function(){
 7     $(":selected").hide();
 8   });
 9 });
10 </script>
11 </head>
12 <body>
13 
14 <select multiple="multiple">
15   <option>Volvo</option>
16   <option selected="selected">Saab</option>
17   <option>Mercedes</option>
18   <option>Audi</option>
19 </select>
20 <br />
21 <button class="btn1">Hide Selected</button>
22 </body>
23 </html>

View
Code

5   表单采用器

5.1 一览表

Bootstrap 114

5.2 测试代码

(1):input()(拔取具有input元素)

Bootstrap 115Bootstrap 116

 1 <!DOCTYPE html>
 2 
 3 <html>
 4 <head>
 5     <meta name="viewport" content="width=device-width" />
 6     <script src="~/Scripts/jquery-1.10.2.js"></script>
 7     <title>JQuery函数</title>
 8     <script type="text/javascript">
 9         $(document).ready(function () {
10             $(":input").css("background-color", "#B2E0FF");
11         });
12     </script>
13 </head>
14 <body>
15     <form action="">
16         Name: <input type="text" name="user" />
17         <br />
18         Password: <input type="password" name="password" />
19         <br />
20         <button type="button">Useless Button</button>
21         <input type="button" value="Another useless button" />
22         <br />
23         <input type="reset" value="Reset" />
24         <input type="submit" value="Submit" />
25         <br />
26     </form>
27 </body>
28 </html>

View
Code

测试结果:

Bootstrap 117

(2):text(拔取所有text元素)

Bootstrap 118Bootstrap 119

 1 <!DOCTYPE html>
 2 
 3 <html>
 4 <head>
 5     <meta name="viewport" content="width=device-width" />
 6     <script src="~/Scripts/jquery-1.10.2.js"></script>
 7     <title>JQuery函数</title>
 8     <script type="text/javascript">
 9         $(document).ready(function () {
10             $(":text").css("background-color", "#B2E0FF");
11         });
12     </script>
13 </head>
14 <body>
15     <form action="">
16         Name: <input type="text" name="user" />
17         <br />
18         Password: <input type="password" name="password" />
19         <br />
20         <button type="button">Useless Button</button>
21         <input type="button" value="Another useless button" />
22         <br />
23         <input type="reset" value="Reset" />
24         <input type="submit" value="Submit" />
25         <br />
26     </form>
27 </body>
28 </html>

View
Code

测试结果:

Bootstrap 120

(3):select和:button

(4)其他表单元素相比较简单,在此不列举。

6   参考文献

【01】http://www.w3school.com.cn/jquery/jquery\_ref\_selectors.asp

【02】http://jqueryui.com/

【03】http://www.cnblogs.com/hulang/archive/2011/01/12/1933771.html

【04】http://www.jb51.net/article/57753.htm

【05】http://www.jquery123.com/

 

7   版权

 

  • 感谢您的阅读,若有不足之处,欢迎指教,共同学习、共同提高。
  • 博主网址:http://www.cnblogs.com/wangjiming/。
  • 极少一些小说利用读书、参考、引用、抄袭、复制和粘贴等多种艺术结合而成的,大部分为原创。
  • 如您喜爱,麻烦推荐一下;如您有新想法,欢迎提议,邮箱:2016177728@qq.com。
  • 可以转载该博客,但无法不闻明博客来源。

相关文章