jQuery初學(xué):find()方法及children方法的區(qū)別分析
更新時(shí)間:2011年01月31日 23:19:42 作者:
總經(jīng)一下前段時(shí)間用于的jQuery方法:find及children。需要的朋友可以參考下。
首先看看英文解釋吧:
children方法:

find方法:

通過以上的解釋,可以總結(jié)如下:
1:children及find方法都用是用來獲得element的子elements的,兩者都不會(huì)返回 text node,就像大多數(shù)的jQuery方法一樣。
2:children方法獲得的僅僅是元素一下級(jí)的子元素,即:immediate children。
3:find方法獲得所有下級(jí)元素,即:descendants of these elements in the DOM tree
4:children方法的參數(shù)selector 是可選的(optionally),用來過濾子元素,但find方法的參數(shù)selector方法是必選的。
5:find方法事實(shí)上可以通過使用 jQuery( selector, context )來實(shí)現(xiàn):英語如是說:Selector context is implemented with the .find() method; therefore, $('li.item-ii').find('li') is equivalent to $('li', 'li.item-ii').
例如,有以下html元素:

使用:$('ul.level-2').children().css('border', '1px solid green'); 的效果是:

使用 $('ul.level-2').find('li').css('border', '1px solid green'); 的效果是:
children方法:

find方法:

通過以上的解釋,可以總結(jié)如下:
1:children及find方法都用是用來獲得element的子elements的,兩者都不會(huì)返回 text node,就像大多數(shù)的jQuery方法一樣。
2:children方法獲得的僅僅是元素一下級(jí)的子元素,即:immediate children。
3:find方法獲得所有下級(jí)元素,即:descendants of these elements in the DOM tree
4:children方法的參數(shù)selector 是可選的(optionally),用來過濾子元素,但find方法的參數(shù)selector方法是必選的。
5:find方法事實(shí)上可以通過使用 jQuery( selector, context )來實(shí)現(xiàn):英語如是說:Selector context is implemented with the .find() method; therefore, $('li.item-ii').find('li') is equivalent to $('li', 'li.item-ii').
例如,有以下html元素:

使用:$('ul.level-2').children().css('border', '1px solid green'); 的效果是:

使用 $('ul.level-2').find('li').css('border', '1px solid green'); 的效果是:

相關(guān)文章
jQuery控制的不同方向的滑動(dòng)(向左、向右滑動(dòng)等)
這篇文章主要介紹了jQuery控制的不同方向的滑動(dòng)(向左、向右滑動(dòng)等),需要的朋友可以參考下2014-07-07基于Bootstrap和JQuery實(shí)現(xiàn)動(dòng)態(tài)打開和關(guān)閉tab頁的實(shí)例代碼
這篇文章主要介紹了基于Bootstrap和JQuery實(shí)現(xiàn)動(dòng)態(tài)打開和關(guān)閉tab頁的實(shí)例代碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-06-06jQuery實(shí)現(xiàn)ajax的疊加和停止(終止ajax請(qǐng)求)
這篇文章主要介紹了jQuery實(shí)現(xiàn)ajax的疊加和停止的關(guān)鍵代碼,代碼簡(jiǎn)單易懂,需要的朋友可以參考下2016-08-08使用jQuery.form.js/springmvc框架實(shí)現(xiàn)文件上傳功能
這篇文章主要介紹了使用jQuery.form.js/springmvc框架實(shí)現(xiàn)文件上傳功能,非常具有參考借鑒價(jià)值,感興趣的朋友一起學(xué)習(xí)吧2016-05-05