jQuery :nth-child()
此選擇器匹配所有父元素下的第N個子元素或奇偶元素。
因為jQuery實現:nth-child(index)是嚴格來自CSS規範,所以index值是從1開始計數。
jQuery1.14版本新增。
語法結構:
[JavaScript] 純文字檢視 複製程式碼jQuery(":nth-child(index/even/odd/equation)")
程式碼例項:
[HTML] 純文字檢視 複製程式碼執行程式碼<!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <meta name="author" content="http://www.softwhy.com/" /> <title>螞蟻部落</title> <script src="http://code.jquery.com/jquery-latest.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("#bt").click(function () { $("li:nth-child(2)").css("color","blue") }) }) </script> </head> <body> <div> <ul> <li>ASP教程</li> <li>ASP.NET教程</li> <li>PHP教程</li> </ul> <ul> <li>HTML教程</li> <li>DIV+CSS教程</li> <li>jQUERY教程</li> <li>jAVAScript教程</li> </ul> </div> <input type="button" value="檢視效果" id="bt" /> </body> </html>
以上程式碼可以將ul下第二個li元素字型顏色設定為藍色。
[HTML] 純文字檢視 複製程式碼<!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <meta name="author" content="http://www.softwhy.com/" /> <title>螞蟻部落</title> <script src="http://code.jquery.com/jquery-latest.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("#bt").click(function () { $("li:nth-child(even)").css("color", "blue") }) }) </script> </head> <body> <div> <ul> <li>ASP教程</li> <li>ASP.NET教程</li> <li>PHP教程</li> </ul> <ul> <li>HTML教程</li> <li>DIV+CSS教程</li> <li>jQUERY教程</li> <li>JavaScript教程</li> </ul> </div> <input type="button" value="檢視效果" id="bt" /> </body> </html>
將排序為偶數的li元素中的字型顏色設定為藍色。
[HTML] 純文字檢視 複製程式碼<!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <meta name="author" content="http://www.softwhy.com/" /> <title>螞蟻部落</title> <script src="http://code.jquery.com/jquery-latest.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("#bt").click(function () { $("li:nth-child(3n+1)").css("color","blue") }) }) </script> </head> <body> <div> <ul> <li>ASP教程</li> <li>ASP.NET教程</li> <li>PHP教程</li> </ul> <ul> <li>HTML教程</li> <li>DIV+CSS教程</li> <li>jQUERY教程</li> <li >jAVAScript教程</li> </ul> </div> <input type="button" value="檢視效果" id="bt" /> </body> </html>
除了直接給出索引值或者even/odd固定值,還可以是表示式,如上面程式碼的3n+1,這裡的n是從0開始的自然數。
[HTML] 純文字檢視 複製程式碼<!doctype html> <html> <head> <meta charset="utf-8"> <meta name="author" content="http://www.softwhy.com/" /> <title>螞蟻部落</title> <script src="http://code.jquery.com/jquery-latest.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("#bt").click(function(){ $("li:nth-child(2)").css("color", "blue"); }); }); </script> </head> <body> <ul> <li>螞蟻部落一</li> <li>螞蟻部落二</li> <li>螞蟻部落三</li> <li>螞蟻部落四</li> </ul> <ul> <li>螞蟻部落一</li> <li>螞蟻部落二</li> <li>螞蟻部落三</li> <li>螞蟻部落四</li> </ul> <input type="button" value="檢視效果" id="bt" /> </body> </html>
此選擇器能夠匹配父元素下指定索引的所有子元素,可以匹配多個元素,而:eq()只能匹配一個。
相關文章
- CSS3偽類選擇器:nth-child()(nth-child(odd)/nth-child(even))CSSS3
- CSS E:nth-child(n)CSS
- 【CSS】nth-child 與 nth-of-type辨析CSS
- nth-child() 選擇器用法詳解
- css3 nth-child選擇器CSSS3
- (?<=p)與:nth-child()的相似性分析
- :nth-child選擇器使用程式碼例項
- nth-of-type()和nth-child()選擇器的區別
- CSS中nth-child和nth-of-type的簡單使用CSS
- nth-child和nth-of-type這倆偽類有毛關係?
- 淚目了!CSS nth-child偽類終於支援了of關鍵詞CSS
- css3 nth-child() 選擇器 (遍歷選擇器的奇偶數)CSSS3
- 教你發現CSS3的"nth-child"和"nth-of-type"的區別!CSSS3
- jQuery初探:自制jQueryjQuery
- 我的’jQuery’和jQueryjQuery
- 我的''jQuery''和jQueryjQuery
- 【Jquery】jquery 基本的動畫jQuery動畫
- 【Jquery】jquery 自定義動畫jQuery動畫
- JqueryjQuery
- jQuery :not()jQuery
- jQuery not()jQuery
- jQuery is()jQuery
- jQuery()jQuery
- jQuery on()jQuery
- JQuery模板外掛-jquery.tmpljQuery
- jquery列印頁面(jquery.jqprint)jQuery
- jQuery入門(三)--- jQuery語法jQuery
- jQuery 3教程(一):jQuery介紹jQuery
- jQuery之使用jQuery.fn.prop()替換jQuery.fn.attr()jQuery
- JQuery基本知識彙總;JQuery常用方法;淺入瞭解JQueryjQuery
- (jQuery) jQuery中的事件與動畫(上)jQuery事件動畫
- jquery.fn.extend與jquery.extendjQuery
- jQuery 1.10.0 和 jQuery 2.0.1 釋出jQuery
- jQuery表單外掛jQuery.formjQueryORM
- jQuery 3教程(二):jQuery選擇器jQuery
- jQuery心得2--jQuery案例剖析1jQuery
- jQuery初探jQuery
- jQuery碎片jQuery