Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> JavaScript

Khác biệt giữa div~div và div:not(:first-of-type)?


Hai bộ chọn này tương đương về mặt khớp phần tử. Xem ví dụ:

<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),    nhưng không phải 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>

Nếu có quy tắc CSS cả hai selector khớp cùng phần tử, div:not(:first-of-type) sẽ ưu tiên do pseudo-class :first-of-type.