1. jQuery 創(chuàng)建的對象都是新對象
alert( $( "#logo" ) === $( "#logo" ) ); // alerts "false"
alert( $( "#logo" ).get(0) === $( "#logo" ).get(0) ); // alerts "true"
盡管兩個(gè) jQuery 對象包含相同的 DOM 元素,但是倆對象并不相等洪橘,要判斷倆對象是否包含相同 DOM 元素,可以通過 get 方法獲取 jQuery 對象包含的 DOM 元素來比較。
2. jQuery 對象并不能自動(dòng)更新
var allParagraphs = $( "p" );
allParagraphs 并不會(huì)隨著頁面上 p 元素的增加或減少而改變关划,除非重新執(zhí)行
allParagraphs = $( "p" );
3. 遍歷 DOM 元素
參考文本
<div class="grandparent">
<div class="parent">
<div class="child">
<span class="subchild"></span>
</div>
</div>
<div class="surrogateParent1"></div>
<div class="surrogateParent2"></div>
</div>
a. 遍歷父節(jié)點(diǎn)
主要有 .parent(), .parents(), .parentsUntil(), 和 .closest() 方法
// Selecting an element's direct parent:
// returns [ div.child ]
$( "span.subchild" ).parent();
// Selecting all the parents of an element that match a given selector:
// returns [ div.parent ]
$( "span.subchild" ).parents( "div.parent" );
// returns [ div.child, div.parent, div.grandparent ]
$( "span.subchild" ).parents();
// Selecting all the parents of an element up to, but *not including* the selector:
// returns [ div.child, div.parent ]
$( "span.subchild" ).parentsUntil( "div.grandparent" );
// Selecting the closest parent, note that only one parent will be selected
// and that the initial element itself is included in the search:
// returns [ div.child ]
$( "span.subchild" ).closest( "div" );
// returns [ div.child ] as the selector is also included in the search:
$( "div.child" ).closest( "div" );
b. 遍歷子節(jié)點(diǎn)
主要有 .children 和 .find 方法
// Selecting an element's direct children:
// returns [ div.parent, div.surrogateParent1, div.surrogateParent2 ]
$( "div.grandparent" ).children( "div" );
// Finding all elements within a selection that match the selector:
// returns [ div.child, div.parent, div.surrogateParent1, div.surrogateParent2 ]
$( "div.grandparent" ).find( "div" );
c. 遍歷兄弟節(jié)點(diǎn)
主要方法有.prev(), .next(), .siblings(), .nextAll(), .nextUntil(), .prevAll() 和 .prevUntil()
// Selecting a next sibling of the selectors:
// returns [ div.surrogateParent1 ]
$( "div.parent" ).next();
// Selecting a prev sibling of the selectors:
// returns [] as No sibling exists before div.parent
$( "div.parent" ).prev();
// Selecting all the next siblings of the selector:
// returns [ div.surrogateParent1, div.surrogateParent2 ]
$( "div.parent" ).nextAll();
// returns [ div.surrogateParent1 ]
$( "div.parent" ).nextAll().first();
// returns [ div.surrogateParent2 ]
$( "div.parent" ).nextAll().last();
// Selecting all the previous siblings of the selector:
// returns [ div.surrogateParent1, div.parent ]
$( "div.surrogateParent2" ).prevAll();
// returns [ div.surrogateParent1 ]
$( "div.surrogateParent2" ).prevAll().first();
// returns [ div.parent ]
$( "div.surrogateParent2" ).prevAll().last();
// Selecting an element's siblings in both directions that matches the given selector:
// returns [ div.surrogateParent1, div.surrogateParent2 ]
$( "div.parent" ).siblings();
// returns [ div.parent, div.surrogateParent2 ]
$( "div.surrogateParent1" ).siblings();