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.

">
立诚勿怠,格物致知
It's all about connecting the dots

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》
文章链接:https://www.orzzone.com/dom-traversal-methods-in-jquery.html
商业联系:yakima.public@gmail.com

本站大部分文章为原创或编译而来,对于本站版权文章,未经许可不得用于商业目的,非商业性转载请以链接形式标注原文出处。
本站内容仅供个人学习交流,不做为任何投资、建议的参考依据,因此产生的问题需自行承担。

评论 抢沙发

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

非常感谢你的打赏,我们将继续给力提供更多优质内容!

支付宝扫一扫打赏

微信扫一扫打赏

登录

找回密码

注册