jQuery 過(guò)濾
jQuery 遍歷- 過(guò)濾
縮寫(xiě)搜索元素的范圍
三個(gè)最基本的過(guò)濾方法是:first(), last() 和 eq(),它們?cè)试S您基于其在一組元素中的位置來(lái)選擇一個(gè)特定的元素。
其他過(guò)濾方法,比如 filter() 和 not() 允許您選取匹配或不匹配某項(xiàng)指定標(biāo)準(zhǔn)的元素。
jQuery first() 方法
first() 方法返回被選元素的首個(gè)元素。
下面的例子選取首個(gè) <div> 元素內(nèi)部的第一個(gè) <p> 元素:
實(shí)例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"> </script> <script> $(document).ready(function(){ $("div p").first().css("background-color","yellow"); }); </script> </head> <body> <h1>歡迎訪問(wèn)我的主頁(yè)</h1> <div> <p>這是 div 中的一個(gè)段落。</p> </div> <div> <p>這是另外一個(gè) div 中的一個(gè)段落。</p> </div> <p>這是一個(gè)段落。</p> </body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
jQuery last() 方法
last() 方法返回被選元素的最后一個(gè)元素。
下面的例子選擇最后一個(gè) <div> 元素中的最后一個(gè) <p> 元素:
實(shí)例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"> </script> <script> $(document).ready(function(){ $("div p").last().css("background-color","yellow"); }); </script> </head> <body> <h1>歡迎訪問(wèn)我的主頁(yè)</h1> <div> <p>這是 div 中的一個(gè)段落。</p> </div> <div> <p>這是另外一個(gè) div 中的一個(gè)段落。</p> </div> <p>這是一個(gè)段落。</p> </body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
jQuery eq() 方法
eq() 方法返回被選元素中帶有指定索引號(hào)的元素。
索引號(hào)從 0 開(kāi)始,因此首個(gè)元素的索引號(hào)是 0 而不是 1。下面的例子選取第二個(gè) <p> 元素(索引號(hào) 1):
實(shí)例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"> </script> <script> $(document).ready(function(){ $("p").eq(1).css("background-color","yellow"); }); </script> </head> <body> <h1>歡迎訪問(wèn)我的主頁(yè)</h1> <p>菜鳥(niǎo)教程 (index 0).</p> <p>http://www.runoob.com (index 1)。</p> <p>google (index 2).</p> <p>http://www.google.com (index 3)。</p> </body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
jQuery filter() 方法
filter() 方法允許您規(guī)定一個(gè)標(biāo)準(zhǔn)。不匹配這個(gè)標(biāo)準(zhǔn)的元素會(huì)被從集合中刪除,匹配的元素會(huì)被返回。
下面的例子返回帶有類(lèi)名 "url" 的所有 <p> 元素:
實(shí)例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"> </script> <script> $(document).ready(function(){ $("p").filter(".url").css("background-color","yellow"); }); </script> </head> <body> <h1>歡迎訪問(wèn)我的主頁(yè)</h1> <p>菜鳥(niǎo)教程 (index 0).</p> <p class="url">http://www.runoob.com (index 1)。</p> <p>google (index 2).</p> <p class="url">http://www.google.com (index 3)。</p> </body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
jQuery not() 方法
not() 方法返回不匹配標(biāo)準(zhǔn)的所有元素。
提示:not() 方法與 filter() 相反。
下面的例子返回不帶有類(lèi)名 "url" 的所有 <p> 元素:
實(shí)例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"> </script> <script> $(document).ready(function(){ $("p").not(".url").css("background-color","yellow"); }); </script> </head> <body> <h1>歡迎訪問(wèn)我的主頁(yè)</h1> <p>菜鳥(niǎo)教程 (index 0).</p> <p class="url">http://www.runoob.com (index 1)。</p> <p>google (index 2).</p> <p class="url">http://www.google.com (index 3)。</p> </body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例