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)文章
用Juery網(wǎng)頁選項(xiàng)卡實(shí)現(xiàn)代碼
現(xiàn)在jquery比較流行,很多的選項(xiàng)卡效果可以通過jquery來實(shí)現(xiàn)。2011-06-06
jQuery獲取標(biāo)簽文本內(nèi)容和html內(nèi)容的方法
這篇文章主要介紹了jQuery獲取標(biāo)簽文本內(nèi)容和html內(nèi)容的方法,實(shí)例分析了jQuery中text和html方法的使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03
jQuery實(shí)現(xiàn)input[type=file]多圖預(yù)覽上傳刪除等功能
這篇文章主要介紹了jQuery實(shí)現(xiàn)input[type=file]多圖預(yù)覽上傳刪除等功能,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-08-08
Jquery工作常用實(shí)例 使用AJAX使網(wǎng)頁進(jìn)行異步更新
AJAX 通過在后臺(tái)與服務(wù)器交換少量數(shù)據(jù)的方式,允許網(wǎng)頁進(jìn)行異步更新。這意味著有可能在不重載整個(gè)頁面的情況下,對(duì)網(wǎng)頁的一部分進(jìn)行更新。2011-07-07
jQuery實(shí)現(xiàn)公告文字左右滾動(dòng)的實(shí)例代碼
這篇文章介紹了jQuery實(shí)現(xiàn)公告文字左右滾動(dòng)的實(shí)例代碼,有需要的朋友可以參考一下2013-10-10

