jQuery .closest()
.closest()
.closest( selector )Returns: jQuery
Description: For each element in the set, get the first element that matches the selector by testing the element itself and traversing up through its ancestors in the DOM tree.
-
version added: 1.3.closest( selector )
-
selectorType: SelectorA string containing a selector expression to match elements against.
-
-
version added: 1.4.closest( selector [, context ] )
-
version added: 1.6.closest( selection )
-
selectionType: jQueryA jQuery object to match elements against.
-
-
version added: 1.6.closest( element )
-
elementType: ElementAn element to match elements against.
-
Given a jQuery object that represents a set of DOM elements, the .closest()
method searches through these elements and their ancestors in the DOM tree and constructs a new jQuery object from the matching elements.
The .parents()
and .closest()
methods are similar in that they both traverse up the DOM tree.
The differences between the two, though subtle, are significant:
.closest()
Begins with the current element
Travels up the DOM tree until it finds a match for the supplied selector
The returned jQuery object contains zero or one element for each element in the original set, in document order
.parents()
Begins with the parent element
Travels up the DOM tree to the document's root element, adding each ancestor element to a temporary collection; it then filters that collection based on a selector if one is supplied
The returned jQuery object contains zero or more elements for each element in the original set, in reverse document order
<ul id="one" class="level-1"> <li class="item-i">I</li> <li id="ii" class="item-ii">II <ul class="level-2"> <li class="item-a">A</li> <li class="item-b">B <ul class="level-3"> <li class="item-1">1</li> <li class="item-2">2</li> <li class="item-3">3</li> </ul> </li> <li class="item-c">C</li> </ul> </li> <li class="item-iii">III</li> </ul>
Suppose we perform a search for <ul>
elements starting at item A:
$( "li.item-a" ) .closest( "ul" ) .css( "background-color", "red" );
This will change the color of the level-2 <ul>
, since it is the first encountered when traveling up the DOM tree.
Suppose we search for an <li>
element instead:
$( "li.item-a" ) .closest( "li" ) .css( "background-color", "red" );
This will change the color of list item A. The .closest()
method begins its search with the element itself before progressing up the DOM tree, and stops when item A matches the selector.
We can pass in a DOM element as the context within which to search for the closest element.
var listItemII = document.getElementById( "ii" ); $( "li.item-a" ) .closest( "ul", listItemII ) .css( "background-color", "red" ); $( "li.item-a" ) .closest( "#one", listItemII ) .css( "background-color", "green" );
This will change the color of the level-2 <ul>
, because it is both the first <ul>
ancestor of list item A and a descendant of list item II. It will not change the color of the level-1 <ul>
, however, because it is not a descendant of list item II.
作者:Chuck Lu GitHub |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2017-06-28 .net framework tools
2016-06-28 open Command window here
2016-06-28 Adding a model