国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

JQuery Tips(3) 關于$()包裝集內元素的改變

2019-11-21 00:54:35
字體:
來源:轉載
供稿:網友
這兩個方法是比較容易搞混的.
filter方法表示的是對當前內部的元素進行篩選,這個接受兩種參數,一個返回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加通配符的方法可以返回除了文本節點之外的所有子元素:
復制代碼 代碼如下:

<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(“>*也會產生同樣的效果”).若想采納所有的直系子元素直接在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>

我們會發現首先alert two,因為two先被選擇

PS:liver writer代碼高亮插件我一加中文就是亂碼,很郁悶的說-.-!!所以注釋都是鳥語了
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 盐亭县| 绥中县| 绍兴县| 抚顺县| 平定县| 鄄城县| 甘泉县| 永福县| 望江县| 策勒县| 兴海县| 红安县| 广水市| 固安县| 梁山县| 临猗县| 扶余县| 大石桥市| 陆丰市| 叶城县| 开鲁县| 正定县| 阿鲁科尔沁旗| 斗六市| 安顺市| 宁夏| 新巴尔虎右旗| 宁德市| 牙克石市| 兴海县| 宁国市| 五大连池市| 确山县| 永登县| 绍兴市| 来安县| 肃南| 绥宁县| 屯留县| 宜宾县| 无为县|