JQuery Tips(3) 關于$()包裝集內元素的改變
更新時間:2009年12月14日 23:17:38 作者:
JQuery包裝集內的元素在一開始的選定后,還可以通過一系列JQuery提供的方法對包裝集內的元素進行擴充,修改,篩選,刪除find()方法 VS filter()方法
這兩個方法是比較容易搞混的.
filter方法表示的是對當前內部的元素進行篩選,這個接受兩種參數(shù),一個返回bool的function,或者是JQuery的選擇表達式,包裝集內的元素只會小于等于當前包裝集內的元素,并且含有的元素屬于原來包裝集內元素的子集:
<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方法卻是在當前元素內(子元素)部進行查找,并返回新的包裝集,這意味著包裝集可能會增加:
<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>
從上面可以看出新包裝集內的元素增加了
parents()方法 VS closest()方法
這兩個方法都是由當前元素向上查找所匹配的元素,不同之處如下:
<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方法來說,會將當前元素向上的所有匹配元素加入新的包裝集并返回,而closest方法只會包含離當前元素最近的元素,所以使用closest方法后當前包裝集內的元素只能為1個或者0個
而parents方法并不包括當前包裝集內的元素,而closest方法會包含當前包裝集內的元素
直系子元素 VS 所有子元素
使用children可以返回直系子元素,而用find加通配符的方法可以返回除了文本節(jié)點之外的所有子元素:
<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方法只會含有當前元素的直系子元素,而使用find(“>*也會產(chǎn)生同樣的效果”).若想采納所有的直系子元素直接在find內傳”*”通配符
回到過去的end()方法以及andself()方法
上述所有的方法,以及比如add(),next(),nextAll(),prev()等對包裝集內元素進行改變的方法都可以使用end()方法來進行返回:
<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()方法總是和最近的一個和包裝集改變的方法相抵消,而抵消其他方法:
<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>
如果需要在改變包裝集內元素的情況下還需要包含原始的包裝集內元素,使用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>
我們會發(fā)現(xiàn)首先alert two,因為two先被選擇
PS:liver writer代碼高亮插件我一加中文就是亂碼,很郁悶的說-.-!!所以注釋都是鳥語了
filter方法表示的是對當前內部的元素進行篩選,這個接受兩種參數(shù),一個返回bool的function,或者是JQuery的選擇表達式,包裝集內的元素只會小于等于當前包裝集內的元素,并且含有的元素屬于原來包裝集內元素的子集:
復制代碼 代碼如下:
<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方法卻是在當前元素內(子元素)部進行查找,并返回新的包裝集,這意味著包裝集可能會增加:
復制代碼 代碼如下:
<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>
從上面可以看出新包裝集內的元素增加了
parents()方法 VS closest()方法
這兩個方法都是由當前元素向上查找所匹配的元素,不同之處如下:
復制代碼 代碼如下:
<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方法來說,會將當前元素向上的所有匹配元素加入新的包裝集并返回,而closest方法只會包含離當前元素最近的元素,所以使用closest方法后當前包裝集內的元素只能為1個或者0個
而parents方法并不包括當前包裝集內的元素,而closest方法會包含當前包裝集內的元素
直系子元素 VS 所有子元素
使用children可以返回直系子元素,而用find加通配符的方法可以返回除了文本節(jié)點之外的所有子元素:
復制代碼 代碼如下:
<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方法只會含有當前元素的直系子元素,而使用find(“>*也會產(chǎn)生同樣的效果”).若想采納所有的直系子元素直接在find內傳”*”通配符
回到過去的end()方法以及andself()方法
上述所有的方法,以及比如add(),next(),nextAll(),prev()等對包裝集內元素進行改變的方法都可以使用end()方法來進行返回:
復制代碼 代碼如下:
<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()方法總是和最近的一個和包裝集改變的方法相抵消,而抵消其他方法:
復制代碼 代碼如下:
<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>
如果需要在改變包裝集內元素的情況下還需要包含原始的包裝集內元素,使用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>
我們會發(fā)現(xiàn)首先alert two,因為two先被選擇
PS:liver writer代碼高亮插件我一加中文就是亂碼,很郁悶的說-.-!!所以注釋都是鳥語了
相關文章
jquery實現(xiàn)的超出屏幕時把固定層變?yōu)槎ㄎ粚拥拇a
相信很多人都上過taobao吧,在taobao的產(chǎn)品列表頁有一個浮動的用來排序的浮動層,當你拖動滾動條而導致那個排序欄看不到的時候它會自動變?yōu)楦訉樱恢惫潭ㄔ谀抢铩?/div> 2010-02-02html5以及jQuery實現(xiàn)本地圖片上傳前的預覽代碼實例講解
這篇文章主要介紹了html5以及jQuery實現(xiàn)本地圖片上傳前的預覽代碼實例講解,圖文代碼實例講解的很清晰,有感興趣的同學可以研究下2021-03-03jQuery對話框插件ArtDialog在雙擊遮罩層時出現(xiàn)關閉現(xiàn)象的解決方法
這篇文章主要介紹了jQuery對話框插件ArtDialog在雙擊遮罩層時出現(xiàn)關閉現(xiàn)象的解決方法,涉及針對插件源碼的修改,需要的朋友可以參考下2016-08-08jquery focus(fn),blur(fn)方法實例代碼
jquery focus(fn),blur(fn)方法實例代碼,需要的朋友可以參考下。2011-12-12jquery.form.js框架實現(xiàn)文件上傳功能案例解析(springmvc)
這篇文章主要為大家詳細介紹了jquery.form.js/springmvc文件上傳功能的實現(xiàn)步驟,使用的技術有jquery.form.js框架,以及springmvc框架,具有實用價值,感興趣的小伙伴們可以參考一下2016-05-05基于jQuery實現(xiàn)仿搜狐辯論投票動畫代碼(附源碼下載)
這篇文章主要介紹了基于jQuery實現(xiàn)仿搜狐辯論投票動畫代碼 的相關資料,需要的朋友可以參考下2016-02-02最新評論