div~div 和 div:not(:first-of-type) 之間的區別?


就匹配元素而言,兩者相同。讓我們來看一個例子。

<section>
   <div></div> <!-- div:first-child or div:first-of-type -->
   <div></div> <!-- div+div or div~div or div:nth-of-type(2) -->
   <p></p>
   <div></div> <!-- div+p+div or div~div or div:nth-of-type(3),
   but not div+div -->
</section>
<section>
   <h1></h1> <!-- h1:first-child -->
   <div></div> <!-- div:first-of-type or div:nth-child(2) -->
   <div></div> <!-- div~div or div:nth-of-type(2) or div:nth-child(3) -->
</section>

如果你的 CSS 規則中兩個選擇器都匹配相同的元素,那麼由於 :first-of-type 偽類,你的 div:not(:first-of-type) 將具有優先順序。

更新於:2020年1月28日

103 次瀏覽

開啟你的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.