Faster, Higher, Stronger
更快,更高,更强

DOM traversal methods in jQuery

注:本文内容摘录自《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.
赞(0) 打赏
未经允许不得转载:峰间的云 » DOM traversal methods in jQuery

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏