標籤:
一、基本選取器
1.ID選取器 $("#id")
2.類別選取器 $(".class")
3.元素選取器 $("element")
4.通配選取器 $("*")
5.集合選取器 $("#id,p,span")
二、層次選取器
1.$("ancestor descendant") 選取ancestor元素裡的所有descendant(後代)元素
2.$("parent>child") 選取parent 元素下的child(子)元素,與後代元素的區別是後代元素包括子項目,孫子項目,重孫子項目.
3.$("prev+next") 等價於$("prev").next("next") 選取緊接在prev元素後的next元素
4.$("prev~siblings") 等價於$("prev").nextAll() 即為之後的所有同輩節點 選取prev元素之後的所有siblings元素
三、過濾選取器
1.基本過濾選取器
:first 選取第1個元素
:last 選取最後一個元素
:not(selector) $("input:not(.myClass)") 選取所有class不是myClass的<input>元素
:even $("input:even") 選取索引是偶數的<input>元素 索引從0開始
:odd 索引是奇數的所有元素,從0開始
:eq(index) 選取索引等於index的元素(index從0開始)
:gt(index) 索引大於index的元素 (index從0開始)
:lt(index) 索引小雨index的元素(index從0開始)
:header 選取所有的標題元素,例如h1,h2等
:animated 選取當前正在執行動畫的所有元素
2.內容過濾選取器
:contains(text) 選取含有常值內容為“text”的元素
:empty 選取不包含子項目或者文本的空元素
:has(selector) 選取含有選取器所匹配的元素的元素
:parent 選擇含有子項目或者文本的元素
3.可見度過濾選取器
:hidden 選取所有不可見的元素 $(":hidden") 包括<input type="hidden"/> <div style="display:none;"><div style="visibility:hidden">
:visible 選取所有可見的元素 $("div:visible") 選取所有可見的div
4.屬性過濾選取器
[attribute] $("div[id]") 選取擁有屬性id的元素
[attribute=value] $("div[title=test]") 選取屬性title為“test”的<div> 元素
[attribute!=value] $("div[title!=test]")選取屬性title不為“test”的<div> 元素,沒有title屬性也會被選中
[attribute^=value] $("div[title^=test]") 選取屬性title以“test"開始的div元素
[attribute$=value] $("div[title$=test]") 選取屬性title以“test"結束的div元素
[attribute*=value] $("div[title*=test]") 選取屬性title含有”test“的div元素
[selector1][selector2][selectorN] $("div[id][title$=‘test‘]") 選取擁有屬性id,並且屬性title以”test“結束的div元素
5.子項目過濾選取器
:nth-child(index/even/odd/equation) :eq(index)只匹配一個元素 ,而:nth-child薑維每一個父元素匹配子項目,並且:nth-child(index)的index是從1開始算起的,而: eq(index)是從0開始
:first-child 每個父元素的第1個子項目
:last-child 每個父元素的最後一個子項目
:only-child 如果某元素是它父元素中唯一的子項目,那麼將會被匹配。如果父元素中含有其他元素,則不會被匹配
jQuery選取器(上)