關於jquery懸著器中:first和:first-child和:first-of-type的區別

來源:互聯網
上載者:User

標籤:style   blog   http   color   io   sp   div   2014   on   

:first:選擇第一個出現符合的元素

:first-child:選擇限制條件中的第一個元素,並且必須和冒號前面的標籤一致

:first-of-type:選擇所有限制條件下的第一個冒號前面的標籤元素,此標籤可以不是第一個

測試:first代碼:

<body><a></a><p>1</p><p>2</p><p>3</p><div><a>Hello1</a><p>Hello2</p><p>Hello3</p></div><div><p>Hello4</p><p>Hello5</p><p>Hello6</p></div><div></div><script>    $(function(){        $zzz=$("p:first").text();        alert($zzz);});</script>
</body>

其中結果為:

測試:first-child代碼:

<body><a></a><p>1</p><p>2</p><p>3</p><div><a>Hello1</a><p>Hello2</p><p>Hello3</p></div><div><p>Hello4</p><p>Hello5</p><p>Hello6</p></div><div></div><script>    $(function(){        $zzz=$("p:first-child").text();        alert($zzz);});</script></body>

其中結果為:

測試:first-of-type代碼:

<body><a></a><p>1</p><p>2</p><p>3</p><div><a>Hello1</a><p>Hello2</p><p>Hello3</p></div><div><p>Hello4</p><p>Hello5</p><p>Hello6</p></div><div></div><script>    $(function(){        $zzz=$("p:first-of-type").text();        alert($zzz);});</script></body>

其中結果為:

 

 

怎麼樣,很好理解吧,研究了好半天的呀!

關於jquery懸著器中:first和:first-child和:first-of-type的區別

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.