jQuery find
.find()
.find( selector )Returns: jQuery
Description: Get the descendants of each element in the current set of matched elements, filtered by a selector, jQuery object, or element.
-
version added: 1.0.find( selector )
-
selectorType: SelectorA string containing a selector expression to match elements against.
-
-
version added: 1.6.find( element )
-
elementAn element or a jQuery object to match elements against.
-
Given a jQuery object that represents a set of DOM elements, the .find()
method allows us to search through the descendants后辈 of these elements in the DOM tree and construct a new jQuery object from the matching elements.
The .find()
and .children()
methods are similar, except that the latter only travels a single level down the DOM tree.
The first signature for the .find()
method accepts a selector expression of the same type that we can pass to the $()
function.
The elements will be filtered by testing whether they match this selector.
The expressions allowed include selectors like > p
which will find all the paragraphs that are children of the elements in the jQuery object.
Consider a page with a basic nested list on it:
<ul class="level-1"> <li class="item-i">I</li> <li 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>
If we begin at item II, we can find list items within it:
$( "li.item-ii" ).find( "li" ).css( "background-color", "red" );
The result of this call is a red background on items A, B, 1, 2, 3, and C. Even though item II matches the selector expression, it is not included in the results; only descendants are considered candidates for the match.
Unlike most of the tree traversal methods, the selector expression is required in a call to .find()
.
If we need to retrieve all of the descendant elements, we can pass in the universal selector '*'
to accomplish this.
Selector context is implemented with the .find()
method;
therefore, $( "li.item-ii" ).find( "li" )
is equivalent to $( "li", "li.item-ii" )
.
As of jQuery 1.6, we can also filter the selection with a given jQuery collection or element. With the same nested list as above, if we start with:
var allListElements = $( "li" );
And then pass this jQuery object to find:
$( "li.item-ii" ).find( allListElements );
This will return a jQuery collection which contains only the list elements that are descendants of item II.
Similarly, an element may also be passed to find:
var item1 = $( "li.item-1" )[ 0 ]; $( "li.item-ii" ).find( item1 ).css( "background-color", "red" );
The result of this call would be a red background on item 1.
作者: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:你的「微服务管家」又秀新绝活了
2016-07-01 Controller methods and views
2016-07-01 Working with SQL Server LocalDB
2016-07-01 ASP.NET Razor - C# Variables
2016-07-01 ASP.NET Razor - C# and VB Code Syntax
2015-07-01 Complete The Pattern #1
2015-07-01 Complete The Pattern #2
2015-07-01 Complete The Pattern #6 - Odd Ladder