Traversing
1、.eq(n):索引n的元素
ex:$("li").eq(1) :获得位于index=1处的<li>元素,index从0开始
<ul>
<li>a</li> // index = 0
<li>b</li><!--matched-->
<li>c</li>
</ul>
ex: $("div").eq(2) :索引为2的div元素
<div id='someID'>div-1</div> //index = 0
<p>p-1</p>
<div class='someClass'>div-2</div> //index = 1
<p>p-2</p>
<div>div-3</div> //index = 2<!--matched--->
<span>span-1</span>
2、.filter():过滤元素
e.g.
$("div").filter(criteria)
criteria可选为: { selector: ".className", Function: function(index,element){...}, jQuery Object: $("selector"), DOM element: document.getElementById( "id" )}
ex:$("div").filter("#someID") :will first find all
then filter them using selector to find the one with id='someID' :选择id为someID的div元素
<div id='someID'>div-1</div><!--matched-->
<div class='someClass'>div-2</div>
<p>p-1</p>
<div>div-3</div>
<span>span-1</span>
ex:$("div").filter( function( index ) { return index < 2 } ):will first find all
then filter them using function to find the ones with index < 2 within the matched set.:选择所有的div,然后筛选出index小于2的div
<div id='someID'>div-1</div> // index = 0 within matched set <div class='someClass'>div-2</div> //index = 1 within matched set <p>p-1</p> <div>div-3</div> // index = 2 within matched set <span>span-1</span>
ex:$("div").filter( document.getElementById( "someID" ) ) :will first find all div then filter them using DOM element to find the ones that match DOM element with id='someID':筛选出所有div,然后使用DOM元素过滤出id=someID的div元素
<div id='someID'>div-1</div>
<div class='someClass'>div-2</div>
<p>p-1</p>
<div>div-3</div>
<span>span-1</span>
ex:$("div").filter( $(".someClass") ) will first find all div then filter them using jQuery Object to find the one that match jQuery Object with class='someClass':筛选出所有div,然后使用jQuery对象查找出class=someClass的div元素
<div id='someID'>div-1</div>
<div class='someClass'>div-2</div>
<p>p-1</p>
<div>div-3</div>
<span>span-1</span>
3、
.first():selects first element in the matched set
.last() :selects last element in the matched set.
ex:$("div").first():will first find all div then filter them by selecting first matching element:从匹配集中找出所有div元素,然后找到第一个div元素
<p>p-1</p>
<div id='someID'>div-1</div>
<div class='someClass'>div-2</div>
<div>div-3</div>
<span>span-1</span>
ex: $("div").last() will first find all div then select last matching element:从匹配集中找出所有div元素,然后找到最后一个div元素
<p>p-1</p>
<div id='someID'>div-1</div>
<div class='someClass'>div-2</div>
<div>div-3</div>
<span>span-1</span>
4、
.has("selector"):selector: jQuery selector or DOM element
e.g. $("div").has("p") :will first find all div then select the ones that have at least one inner p element:查找所有的div,然后找出至少包含一个p元素的div元素
<div> <p>p1</p> </div> <div> <span>span</span> </div> <div> <p>p2</p> </div>
.is(selector):selector: { selector: ".className", Function: function(index,element){...}, jQuery Object: $("selector"), DOM element: document.getElementById( "id" )}
e.g. $("div").is("#someID"):will return true if one of the div elements has id='someID'
// return true
<div id="someID">a</div> // this element matches "is" selector
<div class="className">b</div>
<div>c</div>
ex:$("div").is( function(index) { return index > 2 } ) :will return true if one of the div elements is with index > 2
// return false, the max index here is 2
<div id="someID">a</div>// index=0
<div class="className">b</div>// index=1
<div>c</div>// index=2
ex: $("div").eq(2).is($("div:last")) :will return true if element of index=2 within the matched set is equivalent to $("div:last")
// return true
<div id="someID">a</div>// index=0
<div class="className">b</div>// index=1
<div>c</div> // $("div").eq(2) = $("div:last")
ex:$("div").eq(0).is(document.getElementById( "someID" )) :will return true if element of index=2 within the matched set is equivalent to $("div:last").
// return true <div id="someID">a</div>// $("div").eq(0) = document.getElementById( "someID" ) <div class="className">b</div>// index=1 <div>c</div>
.map(Function):Function: function(index,element){...}
e.g. $("div").map( function(index) { return ...} )
ex:$("div").map( function(index){ return $(this).attr("id")+" - "+index }) :will return id and index of each element in an array.
//return ["a - 0", "b - 1", "c - 2"] <div id="a">A</div> <div id="b">B</div> <div id="c">C</div>
.not(selector):selector: { selector: ".className", Function: function(index,element){...}, jQuery Object: $("selector"), DOM element: document.getElementById( "id" )}
ex:
$("div").not("#someID")
<div id="someID">a</div>
<div class="className">b</div>
<div>c</div>
ex:
$("div").not( function(index) { return index < 2 } ) will select all div but NOT the ones with index < 2
<div id="someID">a</div> // index = 0
<div class="className">b</div> // index =1
<div>c</div>
ex:
$("div").not($("div:last")) will select all div but NOT the one that is equivalent to $("div:last").
<div id="someID">a</div> <div class="className">b</div> <div>c</div> // last
ex:
$("div").not(document.getElementById( "someID" )) will select all div but NOT the one that is equivalent to DOM element document.getElementById( "someID" ).
<div id="someID">a</div>
<div class="className">b</div>
<div>c</div>
5、
.slice(start, end):$("selector").slice(start, end) slices matched set into subset specified by start and optional end.
ex:
$("div").slice(0,1) will first find all div then slice the mathced set into subset starting from element at index=0 and ending "but NOT including" elment at index=1:获得所有div元素,然后截取索引从0开始但不包含1的div元素
<div>A</div> // index = 0 <div>B</div> // index = 1 <div>C</div>
ex:
$("div").slice(1) will first find all div then slice the mathced set into subset starting from element at index=1 to the last element:索引从1开始到结尾的所有div元素
<div>A</div> // index = 0 <div>B</div> // index = 1 <div>C</div> // index = 2
ex:
$("div").slice(-2) will first find all div then slice the mathced set into subset starting from element at index=-2 "second element from the last" to the last element:从第-2个开始到结尾的所有div元素,注:最好一个的索引为-1
<div>A</div> // index = 0 & index = -3 from last
<div>B</div> // index = 1 & index = -2 from last
<div>C</div> // index = 2 & index = -1 from last
.children([selector]):selects direct children of each element in the matched set, children can be optionally filtered by selector:从匹配集中筛选出所有符合条件的直接元素
ex:
$("div").children() will first find all div then select children of each div:筛选出所有div然后筛出div的子元素
<div> <span>A-1</span><!--matched--> <span>A-2</span><!--matched--> </div> <div> <p>B-1</p><!--matched--> <span>B-2</span><!--matched--> </div>
ex:
$("div").children("span"): will first find all div then select children of type span:筛选出所有div然后筛出div的span子元素
<div> <span>A-1</span><!--matched--> <span>A-2</span><!--matched--> </div> <div> <p>B-1</p> <span>B-2</span><!--matched--> </div>
.find(selector):selects descendants matching selector.
ex:
$("div").find("*"): will find all descendants of element div
<div>
<span>
<b>a</b>
</span>
</div>
ex:
$("div").find("b") :will find all descendants of type b for each ancestor element div
<div>
<span>
<b>a</b>
</span>
</div>
.closest(selector):for each element in the matched set, selects closest ancestor "parenet or grand-parent..." or the element itself if matching selector:为匹配集中的元素选择最接近的祖先元素或它自身(有可能是自身)
ex:
$("span").closest("p") will first find all span then select closest p ancestor :
<div> <p> <span>A</span> </p> </div>
ex:
$("span").closest("span") will find the closest to which is itself:
<div>
<p>
<span>A</span>
</p>
</div>
6、
.siblings([selector]):selects siblings for each element in the matched set, optionally filtered by selector:查找符合条件的兄弟元素。
ex:
$("span").siblings() :will select all siblings for each element of type span:查找span元素的所有兄弟元素
<div> <span>A</span> <b>B</b> <div> <b>C</b> </div> </div>
ex:
$("span").siblings("b") :will only select siblings of type b for each element of type span:为每个span元素查找类型为b的兄弟元素
<div>
<span>A</span>
<b>B</b><!--matched-->
<div>
<b>C</b>
</div>
</div>
.parent():will find the first matching parent:找到第一个匹配的父元素
ex:
$("b").parent() will find the first direct parent of b:查找b元素的第一个父元素
<div> <span> <b>a</b> </span> <div> <b>b</b> </div> </div>
ex:
$("b").parent("span") will find the first direct span parent of b:查找b元素第一个父元素为span类型的元素
<div> <span> <b>a</b> </span> <div> <b>b</b> </div> </div>
.parents([selector]):will find the matching ancestors:找到所有符合的父元素
ex:
$("b").parents() will find all ancestors of b:b元素的所有祖先元素
<div> <p> <span> <b>a</b> </span> </p> </div>
ex:
$("b").parents("div") will find div ancestors of b:查找b元素类型为div的父元素
<div> <p> <span> <b>a</b> </span> </p> </div>
.parentsUntil([selector], [filter]):will find ancestors matching optional filter up to but not including ancestor matching selector:找到符合filter条件的父亲元素,但不包含selector指定的父元素。而且最高到filter指定的位置
ex:
$("b").parentsUntil("div"): will find ancestors of b up to but not including div ancestor.
<div> // until here <p> <span> <b>a</b> </span> </p> </div>
ex:
$("b").parentsUntil( $("#someID")): will find ancestors of b up to but not including ancestor element with id='someID':
<div> <div id="someID"> // until here <span> <b>a</b> </span> </div> </div>
ex:
$("b").parentsUntil("div", ".className"):will find ancestors of b matching filter "className" up to but not including ancestor of type div
<div> // until here <p> // not matching filter i.e. NO class=className <span class="className"> <b>a</b> </span> </p> </div>
7、兄弟元素
.next([selector]):will select next adjacent sibling, if selector is provided then it should match selector:寻找下一个紧挨的兄弟元素,如果指定selector的话,需要符合selector的要求
ex:
$("span").next() will find next adjacent sibling of each span:寻找紧挨着span元素的兄弟元素
<b>A</b> <span>B</span> <b>C</b> <span>D</span> <div>E</div>
ex:
$("span").next("div") will only find next adjacent sibling of type div for each span:为每个span元素寻找类型为span的元素
<b>A</b>
<span>B</span>
<b>C</b>
<span>D</span>
<div>E</div>
.nextAll([selector]): will select all next siblings, if selector is provided then they should match selector:寻找所有的兄弟元素,如果指定了selector,需要满足selector的要求
ex:
$("span").nextAll() :will find all next siblings of each span:寻找span元素所有的兄弟元素
<div>A</div>
<span>B</span>
<b>C</b>
<div>D</div>
<div>E</div>
ex:
$("span").nextAll("div"): will find all next siblings of type div for each span:寻找span元素后面的div兄弟元素
<div>A</div>
<span>B</span>
<b>C</b>
<div>D</div>
<div>E</div>
.nextUntil([selector], [filter]):will select all next siblings up to but not included the one matching selector, if filter is provided then next siblings should match filter too:
ex:
$("span").nextUntil("div") :will find all next siblings of each span up to but not included the first div sibling:寻找span后面的兄弟元素直到div元素,但不包含div元素
<div>A</div>
<span>B</span>
<b>C</b>
<p>D</p>
<div>E</div> // until first <div>
<div>F</div>
ex:
$("span").nextUntil("div", "p") will find all next siblings of type p for each span up to but not included the first div sibling:寻找span后面的p类型的兄弟元素直到div元素,不包含div元素:
<div>A</div>
<span>B</span>
<b>C</b>
<p>D</p> // of type <p>
<div>E</div> // until first <div>
<div>F</div>
.prev([selector]) :will select prev adjacent sibling, if selector is provided then it should match selector:寻找紧挨的上一个元素
ex:
$("span").prev(): will find previous adjacent sibling of each span
<b>A</b> <span>B</span> <b>C</b> <span>D</span> <div>E</div>
ex:
$("span").prev("div"): will only find previous adjacent sibling of type div for each span
<b>A</b> //prev but NOT div
<span>B</span>
<b>C</b>
<div>D</div>
<span>E</span>
.prevAll([selector]) :will select all previous siblings, if selector is provided then they should match selector.
ex:
$("span").prevAll() :will find all previous siblings of each span
<b>A</b>
<div>B</div>
<div>C</div>
<span>D</span>
<div>E</div>
ex:
$("span").prevAll("div") will find all previous siblings of type div for each span
<b>A</b>
<div>B</div>
<div>C</div>
<span>D</span>
<div>E</div>
.prevUntil([selector], [filter]) :will select all previous siblings up to but not included the one matching selector, if filter is provided then previous siblings should match filter too:
ex:
$("span").prevUntil("div") will find all previous siblings of each up to but not included the first div sibling:寻找span元素的前面的元素,直到div,但不包含div元素
<div>A</div>
<div>B</div> // until first <div>
<b>C</b>
<p>D</p>
<span>E</span>
<div>F</div>
ex:
$("span").prevUntil("div", "p") will find all previous siblings of type p for each span up to but not included the first div sibling:寻找span元素p元素类型的兄弟元素直到div元素,但不包含div元素
<div>A</div>
<div>B</div> // until first <div>
<b>C</b> // NOT of type <p>
<p>D</p> // of type <p>
<span>E</span>
<div>F</div>