JQuery Tips(3) 關(guān)于$()包裝集內(nèi)元素的改變
filter方法表示的是對當(dāng)前內(nèi)部的元素進(jìn)行篩選,這個(gè)接受兩種參數(shù),一個(gè)返回bool的function,或者是JQuery的選擇表達(dá)式,包裝集內(nèi)的元素只會(huì)小于等于當(dāng)前包裝集內(nèi)的元素,并且含有的元素屬于原來包裝集內(nèi)元素的子集:
<div id="one">the one</div>
<div id="two"><p>the two</p></div>
<div id="three"><p>the three</p></div>
<script type="text/javascript">
alert($("div").filter(":not(:first):not(:last)").html()); //out put<p>the two</p>
alert($("div").filter(function() { return this.id == "two"; }).html());//output <p>the two</p> as well
</script>
而find方法卻是在當(dāng)前元素內(nèi)(子元素)部進(jìn)行查找,并返回新的包裝集,這意味著包裝集可能會(huì)增加:
<div id="one">the one</div>
<div id="two"><p>the two</p><p></p><p></p></div>
<div id="three"><p>the three</p></div>
<script type="text/javascript">
alert($("div").find("p").text()); //alert "the twothe three"
alert($("div").find("p").length); //alert 4 instead of original 3
</script>
從上面可以看出新包裝集內(nèi)的元素增加了
parents()方法 VS closest()方法
這兩個(gè)方法都是由當(dāng)前元素向上查找所匹配的元素,不同之處如下:
<div id="wrapper">
<div id="two">
<p id="p1">
the two</p>
</div>
</div>
<script type="text/javascript">
alert($("#p1").parents("div").length); //alert 2 include <div id="two"> and <div id="wrapper">
alert($("#p1").closest("div").length); //alert 1 and only include <div id="two">
alert($("#p1").parents("p").length); //alert 0 because it does not include current element
alert($("#p1").closest("p").length); //alert 1 because it contain itself <p id="p1">
</script>
對于parents方法來說,會(huì)將當(dāng)前元素向上的所有匹配元素加入新的包裝集并返回,而closest方法只會(huì)包含離當(dāng)前元素最近的元素,所以使用closest方法后當(dāng)前包裝集內(nèi)的元素只能為1個(gè)或者0個(gè)
而parents方法并不包括當(dāng)前包裝集內(nèi)的元素,而closest方法會(huì)包含當(dāng)前包裝集內(nèi)的元素
直系子元素 VS 所有子元素
使用children可以返回直系子元素,而用find加通配符的方法可以返回除了文本節(jié)點(diǎn)之外的所有子元素:
<div id="wrapper">
text node here
<div id="two">
<p id="p1">
the two</p>
</div>
</div>
<script type="text/javascript">
alert($("#wrapper").children().length);//alert 1 because only direct children included
alert($("#wrapper").find("*").length); //alert 2 because all desendants included
alert($("#wrapper").find(">*").length);//alert 1 because only direct children included
</script>
可以看出children方法只會(huì)含有當(dāng)前元素的直系子元素,而使用find(“>*也會(huì)產(chǎn)生同樣的效果”).若想采納所有的直系子元素直接在find內(nèi)傳”*”通配符
回到過去的end()方法以及andself()方法
上述所有的方法,以及比如add(),next(),nextAll(),prev()等對包裝集內(nèi)元素進(jìn)行改變的方法都可以使用end()方法來進(jìn)行返回:
<div id="wrapper">
text node here
<div id="two">
<p id="p1">
the two</p>
</div>
</div>
<script type="text/javascript">
alert($("#wrapper").find(">*").end().get(0).id);//alert "wrapper" instead of "two" because of end() method has been used
</script>
end()方法總是和最近的一個(gè)和包裝集改變的方法相抵消,而抵消其他方法:
<div id="wrapper">
text node here
<div id="two">
<p id="p1">
the two</p>
</div>
</div>
<script type="text/javascript">
alert($("#wrapper").find("#p1").html("new value").end().get(0).id);//alert wrapper because end method
alert($("#p1").text())//alert new value bacause the html method in previous has not been cancelled
</script>
如果需要在改變包裝集內(nèi)元素的情況下還需要包含原始的包裝集內(nèi)元素,使用andself方法:
<div id="wrapper">
text node here
<div id="two">
<p id="p1">
the two</p>
</div>
</div>
<script type="text/javascript">
var $a = $("#wrapper").find("#two").andSelf();
alert($a[0].id);//alert two first
alert($a[1].id);//alert wrapper after that
</script>
我們會(huì)發(fā)現(xiàn)首先alert two,因?yàn)閠wo先被選擇
PS:liver writer代碼高亮插件我一加中文就是亂碼,很郁悶的說-.-!!所以注釋都是鳥語了
相關(guān)文章
jquery實(shí)現(xiàn)的超出屏幕時(shí)把固定層變?yōu)槎ㄎ粚拥拇a
相信很多人都上過taobao吧,在taobao的產(chǎn)品列表頁有一個(gè)浮動(dòng)的用來排序的浮動(dòng)層,當(dāng)你拖動(dòng)滾動(dòng)條而導(dǎo)致那個(gè)排序欄看不到的時(shí)候它會(huì)自動(dòng)變?yōu)楦?dòng)層,一直固定在那里。2010-02-02html5以及jQuery實(shí)現(xiàn)本地圖片上傳前的預(yù)覽代碼實(shí)例講解
這篇文章主要介紹了html5以及jQuery實(shí)現(xiàn)本地圖片上傳前的預(yù)覽代碼實(shí)例講解,圖文代碼實(shí)例講解的很清晰,有感興趣的同學(xué)可以研究下2021-03-03jQuery對話框插件ArtDialog在雙擊遮罩層時(shí)出現(xiàn)關(guān)閉現(xiàn)象的解決方法
這篇文章主要介紹了jQuery對話框插件ArtDialog在雙擊遮罩層時(shí)出現(xiàn)關(guān)閉現(xiàn)象的解決方法,涉及針對插件源碼的修改,需要的朋友可以參考下2016-08-08jquery 屏蔽一個(gè)區(qū)域內(nèi)的所有元素,禁止輸入
有時(shí)候,需要屏蔽一個(gè)div中所有的input類型,使用jquery很簡單有效的完成。2009-10-10JQuery表格拖動(dòng)調(diào)整列寬效果(自己動(dòng)手寫的)
當(dāng)鼠標(biāo)停留在表頭邊框線上時(shí),鼠標(biāo)會(huì)變成表示左右拖動(dòng)的形狀,接著拖動(dòng)鼠標(biāo),會(huì)在表格中出現(xiàn)一條隨鼠標(biāo)移動(dòng)的豎線,最后放開鼠標(biāo),表格列寬會(huì)被調(diào)整2014-09-09jquery focus(fn),blur(fn)方法實(shí)例代碼
jquery focus(fn),blur(fn)方法實(shí)例代碼,需要的朋友可以參考下。2011-12-12jquery.form.js框架實(shí)現(xiàn)文件上傳功能案例解析(springmvc)
這篇文章主要為大家詳細(xì)介紹了jquery.form.js/springmvc文件上傳功能的實(shí)現(xiàn)步驟,使用的技術(shù)有jquery.form.js框架,以及springmvc框架,具有實(shí)用價(jià)值,感興趣的小伙伴們可以參考一下2016-05-05基于jQuery實(shí)現(xiàn)仿搜狐辯論投票動(dòng)畫代碼(附源碼下載)
這篇文章主要介紹了基于jQuery實(shí)現(xiàn)仿搜狐辯論投票動(dòng)畫代碼 的相關(guān)資料,需要的朋友可以參考下2016-02-02