[Switch] Jquery attribute selector (simultaneously a plurality of matching conditions, or with) (with sample)

 

1 Introduction

 

In addition to selecting process do not meet the two conditions, the need to use a plurality of conditions jquery selector matching process, then compiled a non-related conditions, and combinations thereof.

 

As a note-taking.

 

2. Code

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<!DOCTYPE html>
<html>
<head>
     <title>Test multi selection</title>
     <script src= "http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js" ></script>
     <script type= "text/javascript" >
 
     $().ready( function (){
         
         debugger; // open console and click F10
         //多条件选择
         $( '#td1,#td2,p' ).css( 'color' , 'red' );
         //选择内容不是id=td1
         $( 'tbody td:not(#td1)' ).css( 'color' , 'green' );
         //选择条件1 attr1="a1" 和 条件2 attr2="a2"的元素
         $( '[attr1="a1"][attr2="a2"]' ).css( 'color' , 'blue' );
         //选择条件1 attr1="a1" 或者 条件2 attr2="a2"的元素
         $( '[attr1="a1"],[attr2="a2"]' ).css( 'color' , 'yellow' );
 
         //选择不满足 条件1 attr1="a1" 的元素
         $( 'tbody td[attr1!="a1"]' ).css( 'color' , 'purple' );
         
         //选择不满足 条件1 attr1="a1" 或 条件2 attr2="a2"的元素
         $( 'tbody td:not([attr1="a1"],[attr2="a2"])' ).css( 'color' , 'orange' );
         $( 'tbody td[attr1!="a1"][attr2!="a2"]' ).css( 'color' , 'black' );
 
         //选择不满足 条件1 attr1="a1" 和 条件2 attr2="a2"的元素
         $( 'tbody td:not([attr1="a1"][attr2="a2"])' ).css( 'color' , '#ff7700' );
         $( 'tbody td[attr1!="a1"],tbody td[attr2!="a2"]' ).css( 'color' , 'black' );
 
         //选择tboy中td标签attr1!="a1" 和 所有标签中attr2!="a2", 即除了<td id="td3" attr1="a1" attr2="a2">3</td>的其它所有标签元素
         $( 'tbody td[attr1!="a1"],[attr2!="a2"]' ).css( 'color' , 'grey' );
     })
 
     </script>
</head>
<body>
<table border= "1px" >
     <thead>
         <tr>
             <td>One</td>
             <td>Two</td>
             <td>Three</td>
             <td>Four</td>
             <td>Five</td>
         </tr>
     </thead>
     <tbody>
         <tr>
             <td id= "td1" >1</td>
             <td id= "td2" >2</td>
             <td id= "td3"  attr1= "a1"  attr2= "a2" >3</td>
             <td id= "td4"  attr1= "a1" >4</td>
             <td id= "td5" >5</td>
         </tr>
     </tbody>
</table>
<p>I am first paragraph </p>
</body>
</html>  

 

3. 效果

 

 

1
2
//多条件选择
$( '#td1,#td2,p' ).css( 'color' , 'red' );

 

 

1
2
//选择内容不是id=td1
$( 'tbody td:not(#td1)' ).css( 'color' , 'green' ); 

 

 

1
2
//选择条件1 attr1="a1" 和 条件2 attr2="a2"的元素
$( '[attr1="a1"][attr2="a2"]' ).css( 'color' , 'blue' );

 

 

1
2
//选择条件1 attr1="a1" 或者 条件2 attr2="a2"的元素
$( '[attr1="a1"],[attr2="a2"]' ).css( 'color' , 'yellow' );

 

 

1
2
//选择不满足 条件1 attr1="a1" 的元素
$( 'tbody td[attr1!="a1"]' ).css( 'color' , 'purple' );

 

 

1
2
//选择不满足 条件1 attr1="a1" 或 条件2 attr2="a2"的元素
$( 'tbody td:not([attr1="a1"],[attr2="a2"])' ).css( 'color' , 'orange' );

 

 

1
2
//选择不满足 条件1 attr1="a1" 或 条件2 attr2="a2"的元素(等价于上一条)
$( 'tbody td[attr1!="a1"][attr2!="a2"]' ).css( 'color' , 'black' );

 

 

1
2
//选择不满足 条件1 attr1="a1" 和 条件2 attr2="a2"的元素
$( 'tbody td:not([attr1="a1"][attr2="a2"])' ).css( 'color' , '#ff7700' );

 

 

1
2
//选择不满足 条件1 attr1="a1" 和 条件2 attr2="a2"的元素(等价于上一条)
$( 'tbody td[attr1!="a1"],tbody td[attr2!="a2"]' ).css( 'color' , 'black' ); 

 

 

1
2
//选择tboy中td标签attr1!="a1" 和 所有标签中attr2!="a2", 即除了<td id="td3" attr1="a1" attr2="a2">3</td>的其它所有标签元素
$( 'tbody td[attr1!="a1"],[attr2!="a2"]' ).css( 'color' , 'grey' );

 

 

4. 参考

 

http://www.runoob.com/jquery/jquery-ref-selectors.html


---------------------
作者:85541585
来源:CNBLOGS
原文:https://www.cnblogs.com/fuxp/p/9903184.html
版权声明:本文为作者原创文章,转载请附上博文链接!
内容解析By:CSDN,CNBLOG博客文章一键转载插件

Guess you like

Origin www.cnblogs.com/admans/p/11910823.html