注:本文内容摘录自《Learning jQuery (4th Edition)》。
After creating a jQuery object using $(), we can alter the set of matched elements we are working with by calling one of these DOM traversal methods.
Filtering
Filtering
Traversal method |
Returns a jQuery object containing… |
.filter(selector) |
Selected elements that match the given selector. |
.filter(callback) |
Selected elements for which the callback function returns true. |
.eq(index) |
The selected element at the given 0-based index. |
.first() |
The first selected element. |
.last() |
The final selected element. |
.slice(start, [end]) |
Selected elements in the given range of 0-based indices. |
.not(selector) |
Selected elements that do not match the given selector. |
.has(selector) |
Selected elements that have a descendant matching selector. |
Descendants
Descendants
Traversal method |
Returns a jQuery object containing… |
.find(selector) |
Descendant elements that match the selector. |
.contents() |
Child nodes (including text nodes). |
.children([selector]) |
Children nodes, optionally filtered by a selector. |
Siblings
Siblings
Traversal method |
Returns a jQuery object containing… |
.next([selector]) |
The sibling immediately following each selected element, optionally filtered by a selector. |
.nextAll([selector]) |
All siblings following each selected element, optionally filtered by a selector. |
.nextUntil([selector], [filter]) |
All siblings following each selected element up to and not including the first element matching selector, optionally filtered by an additional selector. |
.prev([selector]) |
The sibling immediately preceding each selected element, optionallly filtered by a selector. |
.prevAll([selector]) |
All siblings preceding each selected element, optionally filtered by a selector. |
.prevUntil([selector], [filter]) |
All siblings preceding each selected element up to and not including the first element matching selector, optionally filtered by additional selector. |
.siblings([selector]) |
Allsiblings, optionally filtered by a selector. |
Traversal method |
Returns a jQuery object containing… |
.parent([selector]) |
The parent of each selected element, optionally filtered by a selector. |
.parents([selector]) |
All ancestors, optionally filtered by a selector. |
.parentsUntil([selector], [filter]) |
All ancestors of each selected element up to and not including the first element matching selector, optionally filtered by an additional selector. |
.closest(selector) |
The first element that matches the selector, starting at the selected element and moving up through its ancestors in the DOM tree. |
.offsetParent() |
The positioned parent, either relative or absolute of the first selected element. |
Collection manipulation
Collection manipulation
Traversal method |
Returns a jQuery object containing… |
.add(selector) |
The selected elements, plus any additional elements that match the given selector. |
.addBack() |
The selected elements, plus the previous set of selected elements on the internal jQuery stack. |
.end() |
The previous set of selected elemetns on the internal jQuery stack. |
.map(callback) |
The result of the callback function when called on each selected element. |
.pushStack(elements) |
Add a collection of DOM elements onto the jQuery stack.. |
Working with selected elements
Working with selected elements
Traversal method |
Returns a jQuery object containing… |
.is(selector) |
Determines whether any matched element is matched by the given selector expression. |
.index() |
Gets the index of the matched element in relation to its siblings. |
.index(element) |
Gets the index of the given DOM node within the set of matched elements. |
$.contains(a, b) |
Determines whether DOM node b contains DOM node a. |
.each(callback) |
Iterates over the matched elements, executing callback for each element. |
.length |
Gets the number of matched elements. |
.get() |
Gets an array of DOM nodes corresponding to the matched elements. |
.get(index) |
Gets the DOM node corresponding to the matched element at the given index. |
.toArray() |
Gets an array of DOM nodes corresponding to the matched elements. |
关于作者:楠溪江人,出生于1991年,目前坐标上海。读书时代跳过级、保过送,工作后转过行。2013年本科毕业于北药。看书、码字、敲代码、打羽毛球是我花时间的爱好。曾在某上市药企任国际药品注册岗,现在某高新企业任前端管理岗。