深入學習jQuery選擇器系列第四篇——過濾選擇器之屬性選擇器

前面的話

  屬性過濾選擇器的過濾規則是通過元素的屬性來獲取相應的元素,對應于CSS中的屬性選擇器。屬性過濾選擇器可分為簡單屬性選擇器、具體屬性選擇器和條件屬性選擇器三種。本文將詳細該部分內容

?

簡單屬性選擇器

[attribute]

  [attribute]選擇器選擇擁有該屬性的元素,返回集合元素

//選擇擁有title屬性的所有元素
$('[title]')
//選擇擁有title屬性的所有span元素
$('span[title]')
//選擇同時擁有title屬性和id屬性的所有span元素
$('span[id][title]')
<button id="btn1" style="color: red;">$('[title]')</button>
<button id="btn2" style="color: blue;">$('span[title]')</button>
<button id="btn3" style="color: green;">$('span[id][title]')</button>
<button id="reset">還原</button>
<div><span title="span0">span0</span><span>span1</span><span title="span2">span2</span><i title="i0">i0</i><span id="span3" title="span3">span3</span><i>i1</i>
</div>
<script>
reset.onclick = function(){history.go();}
//選擇擁有title屬性的所有元素,結果是span0、span2、i0、span3
btn1.onclick = function(){$('[title]').css('color','red');}//選擇擁有title屬性的所有span元素,結果是span0、span2、span3
btn2.onclick = function(){$('span[title]').css('color','blue');}//選擇同時擁有title屬性和id屬性的所有span元素,結果是span3
btn3.onclick = function(){$('span[id][title]').css('color','green');}
</script>

  對應于CSS的簡單屬性選擇器?

[title]{color:red;}span[title]{color:blue;}span[id][title]{color:green;}

  如果使用javascript實現類似$('span[id][title]').css('color','green')的效果

var spans = document.getElementsByTagName('span');
for(var i = 0; i < spans.length; i++){if((spans[i].id != '') && (spans[i].title != '')){spans[i].style.color = 'green';}
}

?

具體屬性選擇器

[attribute=value]

  [attribute=value]選擇器選擇屬性值為value的元素,返回集合元素

//選擇class值為test的元素
$('[class="test"]')
//選擇class值為test的span元素
$('span[class="test"]')
//選擇class值為test span的span元素
$('span[class="test span"]')
//選擇class值為span test的span元素
$('span[class="span test"]')

嚴格匹配

  [注意]具體屬性選擇器的匹配屬于嚴格匹配

  【1】$('[class="test"]')匹配class屬性只有test值的情況;而class="test test1"將不會被匹配

  【2】[class="a1 a2"]和[class="a2 a1"]并不相同,它們分別只嚴格匹配class="a1 a2"和class="a2 a1"的元素

<button id="btn1" style="color: red;">$('[class="test"]')</button>
<button id="btn2" style="color: blue;">$('span[class="test"]')</button>
<button id="btn3" style="color: green;">$('span[class="test span"]')</button>
<button id="btn4" style="color: pink;" >$('span[class="span test"]')</button>
<button id="reset">還原</button>
<div><span class="test">span0</span><span>span1</span><span class="span test">span2</span><i class="test">i0</i><span class="test span">span3</span><i>i1</i>
</div>
<script>
reset.onclick = function(){history.go();}
//選擇class屬性只是'test'的所有元素,結果是span0、i0
btn1.onclick = function(){$('[class="test"]').css('color','red');}//選擇class屬性只是'test'的所有span元素,結果是span0
btn2.onclick = function(){$('span[class="test"]').css('color','blue');}//選擇class屬性是'test span'的所有span元素,結果是span3
btn3.onclick = function(){$('span[class="test span"]').css('color','green');}//選擇class屬性是'span test'的所有span元素,結果是span2
btn4.onclick = function(){$('span[class="span test"]').css('color','pink');}
</script>

  對應于CSS選擇器的具體屬性選擇器

[class="test"]{color:red;}span[class="test"]{color:blue;}span[class="test span"]{color:green;}span[class="span test"]{color:pink;}

  如果使用javascript實現類似$('span[class="span test"]').css('color','pink')的功能

var spans = document.getElementsByTagName('span');
for(var i = 0; i < spans.length; i++){if(spans[i].className == 'span test'){spans[i].style.color = 'pink';}
}

id選擇器

  在CSS選擇器,id選擇器和id屬性選擇器并不相同,因為它們的優先級不同。而jQuery選擇器,并沒有優先級的概念,如果兩個選擇器對相同id屬性同時設置,則后面覆蓋前面

<button id="btn1">$('#test')在后</button>
<button id="btn2">$('[id="test"]')在后</button>
<button id="reset">還原</button>
<div id="test" style="height:20px;">測試內容</div>
<script>
reset.onclick = function(){history.go();}
btn1.onclick = function(){$('[id="test"]').css('color','blue');        $('#test').css('color','red');
}
btn2.onclick = function(){$('#test').css('color','red');$('[id="test"]').css('color','blue');    
}
</script>

條件屬性選擇器

  條件屬性選擇器共包括6種,其中[attribute!=value]選擇器是jQuery自己拓展的選擇器

[attribute!=value]

  [attribute!=value]選擇器選擇屬性值不等于value的元素,返回集合元素

  [注意]class="test test1"的元素也符合$('[class!="test"]')的情況,因為屬性選擇器的嚴格匹配機制

[attribute^=value]

  [attribute^=value]選擇器選擇屬性值以value開始的元素,返回集合元素

[attribute$=value]

  [attribute$=value]選擇器選擇屬性值以value結束的元素,返回集合元素

[attribute*=value]

  [attribute*=value]選擇器選擇屬性值包含value的元素,返回集合元素

[attribute|=value]

  [attribute|=value]選擇器選擇屬性值等于value或以value-開頭的元素,返回集合元素

[attribute~=value]

  [attribute~=value]選擇器選擇屬性值用空格分隔的值中包含value的元素,返回集合元素

  [注意]$('[class~="test"]')選擇器包含class="test"的元素的情況

<button id="btn1" style="color: red;">!=</button>
<button id="btn2" style="color: blue;">^=</button>
<button id="btn3" style="color: green;">$=</button>
<button id="btn4" style="color: pink;" >*=</button>
<button id="btn5" style="color: gray;" >|=</button>
<button id="btn6" style="color: orange;" >~=</button>
<button id="reset">還原</button>
<div><span>1</span><span class="test">2</span><span class="test1">3</span><span class="is-test">4</span><span class="test test1">5</span><span class="test-1">6</span>
</div>
<script>
reset.onclick = function(){history.go();}//選擇class屬性不是'test'的所有元素,結果是1、3、4、5、6
btn1.onclick = function(){$('div [class!="test"]').css('color','red');}//選擇class屬性以'test'開始的所有元素,結果是2、3、5、6
btn2.onclick = function(){$('div [class^="test"]').css('color','blue');}//選擇class屬性以'test'結束的所有元素,結果是2、4
btn3.onclick = function(){$('div [class$="test"]').css('color','green');}//選擇class屬性包含'test'的所有元素,結果是2、3、4、5、6
btn4.onclick = function(){$('div [class*="test"]').css('color','pink');}//選擇class屬性等于'test'或以'test-'開頭的所有元素,結果是2、6
btn5.onclick = function(){$('div [class|="test"]').css('color','gray');}//選擇class屬性在用空格分隔的值包含'test'的所有元素,結果是2、5
btn6.onclick = function(){$('div [class~="test"]').css('color','orange');}
</script>

  對應于CSS選擇器中的部分屬性選擇器

  [注意]由于[attribute!=value]是jQuery自己拓展的,所以并沒有對應的CSS選擇器

[class^="test"]{color:blue;}[class$="test"]{color:green;}[class*="test"]{color:pink;}[class!="test"]{color:gray;}[class~="test"]{color:orange;}

  如果使用javascript實現類似$('span[class~="test"]').css('color','orange')的功能

var spans = document.getElementsByTagName('span');
for(var i = 0; i < spans.length; i++){if(/^test\s|\stest\s|\stest$|^test$/.test(spans[i].className)){spans[i].style.color = 'orange';}
}

?

最后

  屬性選擇器的功能十分強大,特別是條件屬性選擇器,基本覆蓋屬性值的各種情況。但在實際中,使用屬性選擇器卻寥寥,可能是因為使用javascript或jQuery多用于改變元素屬性值,所以使用屬性值作為選擇標準并不穩定

  歡迎交流

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/542680.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/542680.shtml
英文地址,請注明出處:http://en.pswp.cn/news/542680.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

c++ scanf讀取_使用scanf()讀取內存地址并在C中打印其值

c scanf讀取Here, we have to input a valid memory address and print the value stored at memory address in C. 在這里&#xff0c;我們必須輸入一個有效的內存地址并在C中打印存儲在內存地址中的值。 To input and print a memory address, we use "%p" format…

python正則匹配_Python正則表達式只匹配一次

我正在嘗試創建一個簡單的降價乳膠轉換器,只是為了學習 python和基本的正則表達式,但我不知道試圖弄清楚為什么下面的代碼不起作用&#xff1a; re.sub (r\[\*\](.*?)\[\*\]: ?(.*?)$, r\\footnote{\2}\1, s, flagsre.MULTILINE|re.DOTALL) 我想轉換像&#xff1a; s "…

Virtual Network (1) - How to use it in a guest

本文將講述一個問題&#xff1a;kvm guest使用libvirt xml定義如何使用virtual network&#xff1f;1&#xff09;nat&#xff0c; route &#xff0c;isolated, open類型在host中定義virtual network會創建一個虛擬的bridge&#xff0c;相當于一個交換機。guest只需要連接到這…

java string做除法_如果用java來實現傳統方式的除法,用String來保存結果,想精確多少位都行,那改怎么做?...

我會加分的&#xff0c;提個思路都行&#xff0c;目前做了個乘法和加法&#xff0c;但是現在對除法沒有什么思路。以下是我編寫的功能&#xff1a;publicclassCalculator{publicstaticStringmulti(Strings1,Strings2){if(s1nu...我會加分的&#xff0c;提個思路都行&#xff0c…

c語言數組的聲明和初始化_C聲明和初始化能力問題和解答

c語言數組的聲明和初始化This section contains aptitude questions and answers on C language Declarations and Initialization. 本節包含有關C語言聲明和初始化的適切性問題和解答。 1) What will be the output of following program ? int main(){int m10;int xprintf(…

python2和python3的默認編碼_python2和python3哪個版本新

Python2 還是 Python3 &#xff1f; py2.7是2.x系列的最后一個版本&#xff0c;已經停止開發&#xff0c;不再增加新功能。2020年終止支持。 所有的最新的標準庫的更新改進&#xff0c;只會在3.x的版本里出現。Python3.0在2008年就發布出來&#xff0c;而2.7作為2.X的最終版本并…

html-css樣式表

一、CSS&#xff1a;Cascading Style Sheet—層疊樣式表&#xff0c;其作用是美化HTML網頁。 樣式表分類&#xff1a;內聯樣式表、內嵌樣式表、外部樣式表 1、內聯樣式表 和HTML聯合顯示&#xff0c;控制精確&#xff0c;但是可重用性差&#xff0c;冗余多。 例如&#xff1a;&…

java 棧 先進后出_棧先進后出,堆先進先出

1.棧(stack)與堆(heap)都是Java用來在Ram中存放數據的地方。與C不同&#xff0c;Java自動管理棧和堆&#xff0c;程序員不能直接地設置棧或堆。2.棧的優勢是&#xff0c;存取速度比堆要快&#xff0c;僅次于直接位于CPU中的寄存器。但缺點是&#xff0c;存在棧中的數據大小與生…

c#給定二維數組按升序排序_在數組中按升序對數字進行排序| 8086微處理器

c#給定二維數組按升序排序Problem: Write a program in 8086 microprocessor to sort numbers in ascending order in an array of n numbers, where size n is stored at memory address 2000 : 500 and the numbers are stored from memory address 2000 : 501. 問題&#xf…

使用python套用excel模板_Python自動化辦公Excel-從表中批量復制粘貼數據到新表

1、模塊安裝 1&#xff09;cmd模式下&#xff1a; pip install -i https://pypi.tuna.tsinghua.edu.cn/simple xlrd pip install -i https://pypi.tuna.tsinghua.edu.cn/simple openpyxl 2&#xff09;如果有安裝Pycharm&#xff0c;則在程序中操作如下&#xff1a; 菜單欄&…

在HubSpot是如何應對Fat JAR困境的

在七月底&#xff0c;Spring Boot和Dropwizard分別發布了1.4和1.0版本&#xff0c;它們都是基于Fat JAR的。隨著人們更多地采用這些框架和微服務架構&#xff0c;Fat JAR成為了通用的部署機制。\\Fat JAR技術會將Java應用的所有依賴打包到一個bundle之中&#xff0c;便于執行&a…

給定數字的b+樹創建_在C ++中找到給定數字中的兩個的下一個和上一個冪

給定數字的b樹創建Problem statement: 問題陳述&#xff1a; Find Next and previous power of two of a given number 查找給定數字中兩個的下一個和上一個冪 Next power of two 下一個二的冪 Example(1):input: 22output: 32 ( as 32 is 2^5)Example(2):input: 54output…

java 字節數組作用_這段java代碼中字節數組b起到了什么作用?

importjava.io.*;importjavax.swing.*;publicclassIOMonitor{publicstaticvoidmain(String[]temp){//TODO自動生成的方法存根byteb[]newbyte[2];try{FileInputStreamfisnewFileInput...import java.io.*;import javax.swing.*;public class IOMonitor {public static void main…

如何查看本地的崩潰log_過年回家,還怕搶不到票?程序員教你如何搶票

2019年接近尾聲&#xff0c;距離春節回家的日子越來越近&#xff0c;26日起&#xff0c;2020年除夕火車票正式開售&#xff0c;搶票大戰也進入白熱化階段。是否為某搶票 App 加速而煩惱&#xff0c;是否為車票“秒光而煩惱”。別慌&#xff0c;作為連“對象”都是 new 出來的程…

獲取列表中包含的元素數 在C#中

Given a list, and we have to count its total number of elements using List.Count property. 給定一個列表&#xff0c;我們必須使用List.Count屬性計算其元素總數 。 C&#xff03;清單 (C# List) A list is used to represent the list of the objects, it is represent…

I00037 虧數(Deficient number)

數論中&#xff0c;若一個正整數除了本身之外所有因子之和比此數自身小&#xff0c;則稱此數為虧數。虧數&#xff08;Deficient number&#xff09;也稱為缺數&#xff0c;參見百度百科_虧數&#xff0c;或參見維基百科的Deficient number。虧數在OEIS中的數列號為A005100。 問…

hashmap轉紅黑樹的閾值為8_面試必考的 HashMap,這篇總結到位了

點擊藍色“JavaKeeper”關注我喲加個“星標”&#xff0c;一起成長&#xff0c;做牛逼閃閃的技術人1 概述HashMap是基于哈希表實現的,每一個元素是一個key-value對,其內部通過單鏈表解決沖突問題,容量不足(超過了閥值)時,同樣會自動增長.HashMap是非線程安全的,只適用于單線程環…

linux用戶組管理命令_Linux用戶和組命令能力問題和解答

linux用戶組管理命令This section contains Aptitude Questions and Answers on Linux User and Group Commands. 本節包含有關Linux用戶和組命令的 Aptitude問答。 1) Which of the following commands is used to create a new user in the Linux operating system? create…

Failed to start firewalld.service: Unit firewalld.service is masked.

2019獨角獸企業重金招聘Python工程師標準>>> FireWall in Centos 7 masked How to resolve the error message belowFailed to issue method call: Unit firewalld.service is masked. The main reason a service is masked is to prevent accidental starting or e…

mysql第二個索引_MySQL高級第二章——索引優化分析

一、SQL性能下降原因1.等待時間長&#xff1f;執行時間長&#xff1f;可能原因&#xff1a;查詢語句寫的不行索引失效(單值索引、復合索引)CREATE INDEX index_user_name ON user(name);(底層做了一個排序)CREATE INDEX index_user_nameEmail ON user(name,email);查詢關聯join…