亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

jQuery子元素過濾器


名稱說明#範(fàn)例
:nth -child(index/even/odd/equation)

符合其父元素下的第N個(gè)子或奇偶元素

':eq(index)' 只符合一個(gè)元素,而這個(gè)將為每一個(gè)父元素匹配子元素。 :nth-child從1開始的,而:eq()是從0算起的!

可以使用:?
nth-child(even)?
:nth-child(odd)?
:nth-child(3n)?
:nth-child(2)?
:nth-child(3n+1)?
:nth-child(3n+2)

在每個(gè)ul 找出第2 個(gè)li:?
$("ul li: nth-child(2)")
:first-child

#符合第一個(gè)子元素

':first' 只符合一個(gè)元素,而此選擇符將為每個(gè)父元素匹配一個(gè)子元素

在每個(gè)ul 中尋找第一個(gè)li:?
$("ul li:first-child ")
:last-child

符合最後一個(gè)子元素

':last'只符合一個(gè)元素,而此選擇符將為每個(gè)父元素匹配一個(gè)子元素


在每個(gè)ul 中找到最後一個(gè)li:?
$("ul li:last- child")
:only-child

如果某個(gè)元素是父親元素中唯一的子元素,那將會(huì)被符合

如果父元素中含有其他元素,那將不會(huì)被符合。


在ul 中尋找是唯一子元素的li:?
$("ul li:only-child")


註:

1、:nth-child(index)從1開始的,而eq(index)是從0開始的,就是說? $(" ul li:nth-child(0)").css("color","re??d")是取得不到相符的元素,只能從1開始,即? $("ul li:nth-child(1) ").css("color","re??d"),而eq(0)可以獲得,同樣都是獲得第一個(gè)子元素

??? :nth-child(even)是獲得偶數(shù)子元素,即第二個(gè),第四個(gè),第六個(gè)...,而:even則是從索引0開始,匹配第二個(gè)索引,第四個(gè)索引...,也就是第一個(gè),第三個(gè),第五個(gè)...,看起來(lái)像都是獲得奇數(shù)項(xiàng),同樣:nth-child(odd)和:odd同樣也是如此?

2、?? :first-child配對(duì)每個(gè)父類別的子元素,而:first則是匹配一個(gè)子元素,?:last-child和last也是這樣

3、only-child:匹配某個(gè)元素是父親元素中唯一的子元素,就是說當(dāng)前子元素是類別中唯一的元素,則符合!

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>無(wú)標(biāo)題頁(yè)</title>
    <script src="http://code.jquery.com/jquery-3.1.1.min.js"></script>
    <script type="text/javascript">
        jQuery(function($){
            //  $("ul li:first-child").css("color","red");
            $("ul li:first").css("color","red");
            // $("ul li:last-child").css("color","red");
            // $("ul li:nth-child(even)").css("color","red");
            // $("ul li:odd").css("color","red");
        })
    </script>
</head>
<body>
<form id="form1" runat="server">
    <div>
        <ul>
            <li>第一個(gè)元素</li>
            <li>第二個(gè)元素</li>
            <li>第三個(gè)元素</li>
            <li>第四個(gè)元素</li>
            <li>第五個(gè)元素</li>
            <li>第六個(gè)元素</li>
        </ul>
        <ul>
            <li>第一個(gè)元素</li>
            <li>第二個(gè)元素</li>
            <li>第三個(gè)元素</li>
            <li>第四個(gè)元素</li>
            <li>第五個(gè)元素</li>
            <li>第六個(gè)元素</li>
        </ul>
    </div>
</form>
</body>
</html>


#
繼續(xù)學(xué)習(xí)
||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>無(wú)標(biāo)題頁(yè)</title> <script src="http://code.jquery.com/jquery-3.1.1.min.js"></script> </head> <script> $(function(){ // 1選取父元素下索引值是偶數(shù)的子元素的索引值(索引值從1開始的) //找到當(dāng)前元素的父元素,在找他下面的子元素 $("span.child:nth-child(even)").css("fontSize","30px"); //2選取父元素下,索引值是奇數(shù)的元素(索引值從1開始) $("span.child:nth-child(odd)").css("color","red"); //3匹配每個(gè)父元素下,索引值為xx的子元素,索引從1開始 $("span.child:nth-child(1)").css("color","blue"); //4匹配每個(gè)父元素的第一個(gè)子元素 $("span.child:first-child").css("fontSize","50px"); //5匹配每個(gè)父元素的第一個(gè)子元素 $("span.child:last-child").css("fontSize","50px"); }) </script> <body> <div class="parent"> <span class="child">1</span> <span class="child">2</span> <span class="child">3</span> <span class="child">4</span> <span class="child">5</span> </div> </body> </html>
提交重置程式碼