ๆญค้ ้ข็”ฑ็คพ็พคๅพž่‹ฑๆ–‡็ฟป่ญฏ่€Œไพ†ใ€‚ไบ†่งฃๆ›ดๅคšไธฆๅŠ ๅ…ฅ MDN Web Docs ็คพ็พคใ€‚

View in English Always switch to English

CSS Selectors

้ธๆ“‡ๅ™จๅฏไปฅๅฎš็พฉๆŸ็ต„ CSS ๆจฃๅผ่ฆๅฅ—็”จๅˆฐๅ“ชไบ›ๅ…ƒ็ด ไธŠใ€‚

ๅŸบๆœฌ้ธๆ“‡ๅ™จ

้€š็”จ้ธๆ“‡ๅ™จ๏ผˆUniversal selector๏ผ‰

็”จไปฅ้ธๆ“‡ๆ‰€ๆœ‰ๅ…ƒ็ด ใ€‚๏ผˆๅฏ้ธ๏ผ‰ๅฏไปฅๅฐ‡ๅ…ถ้™ๅˆถ็‚บ็‰นๅฎš็š„ๅ็จฑ็ฉบ้–“ๆˆ–ๆ‰€ๆœ‰ๅ็จฑ็ฉบ้–“ใ€‚ ่ชžๆณ•: * ns|* *|* ็ฏ„ไพ‹: * ๅฅ—็”จๆ–‡ๆช”ไธญๆ‰€ๆœ‰ๅ…ƒ็ด ใ€‚

ๆจ™็ฑค้ธๆ“‡ๅ™จ๏ผˆType selector๏ผ‰

็”จไปฅ้ธๆ“‡ๆ‰€ๆœ‰็ฌฆๅˆๆŒ‡ๅฎšๆจ™็ฑค็š„ๅ…ƒ็ด ใ€‚ ่ชžๆณ•: elementname ็ฏ„ไพ‹: input ๅฏ้ธๅ‡บไปปไธ€ <input> ๅ…ƒ็ด ใ€‚

้กžๅˆฅ้ธๆ“‡ๅ™จ๏ผˆClass selector๏ผ‰

็”จไปฅ้ธๆ“‡ๆ‰€ๆœ‰็ฌฆๅˆๆŒ‡ๅฎš class ๅฑฌๆ€งๅ€ผ็š„ๅ…ƒ็ด ใ€‚ ่ชžๆณ•: .classname ็ฏ„ไพ‹: .index ๅฏ้ธๅ‡บไปปไธ€ๅซๆœ‰ index ็š„ class ๅฑฌๆ€งๅ€ผไน‹ๅ…ƒ็ด ใ€‚

ID ้ธๆ“‡ๅ™จ๏ผˆID selector๏ผ‰

็”จไปฅ้ธๆ“‡ๆŒ‡ๅฎš id ๅฑฌๆ€งๅ€ผ็š„ๅ…ƒ็ด ใ€‚๏ผˆไธ€ๅ€‹ๆ–‡ไปถไธญ๏ผŒๆฏๅ€‹ ID ๅฑฌๆ€ง้ƒฝๆ˜ฏๅ”ฏไธ€็š„ใ€‚๏ผ‰ ่ชžๆณ•: #idname ็ฏ„ไพ‹: #toc ๆœƒๆฏ”ๅฐๅซๆœ‰ ID ๆ˜ฏ toc ็š„ๅ…ƒ็ด (ๅฏไปฅๅฎš็พฉๆˆ id="toc" ๆˆ–ๅ…ถไป–้กžไผผ็š„ๅฎš็พฉ)ใ€‚

ๅฑฌๆ€ง้ธๆ“‡ๅ™จ๏ผˆAttribute selector๏ผ‰

็”จไปฅ้ธๆ“‡ๆ‰€ๆœ‰็ฌฆๅˆๆŒ‡ๅฎšๅฑฌๆ€ง็š„ๅ…ƒ็ด ใ€‚ ่ชžๆณ•: [attr] [attr=value] [attr~=value] [attr|=value] [attr^=value] [attr$=value] [attr*=value] ็ฏ„ไพ‹: [autoplay] ๅฐ‡ๆœƒๅฅ—็”จๅซๆœ‰ autoplay ๅฑฌๆ€ง็š„ๅ…ƒ็ด ใ€‚(ไธ่ซ–้€™ๅ€‹ๅฑฌๆ€ง็š„ๅ€ผๆ˜ฏไป€้บผ)ใ€‚

ๅˆ†็ต„้ธๆ“‡ๅ™จ

้ธๆ“‡ๅ™จๅˆ—่กจ๏ผˆSelector list๏ผ‰

, ็”จไปฅๅฐ‡ไธๅŒ็š„้ธๆ“‡ๅ™จ็ต„ๅˆ่ตทไพ†็š„ไธ€็จฎๆ–นๆณ•ใ€‚ ่ชžๆณ•: A, B ็ฏ„ไพ‹: div, span ๅฐ‡ๅŒๆ™‚้ธๆ“‡ <div> ๅ’Œ <span> ๅ…ƒ็ด ใ€‚

็ต„ๅˆ้ธๆ“‡ๅ™จ

ๅพŒไปฃ้ธๆ“‡ๅ™จ๏ผˆDescendant combinator๏ผ‰

" " (็ฉบๆ ผ) ็”จไปฅ้ธๆ“‡ๆŸๅ€‹ๅ…ƒ็ด ๅพŒไปฃ็š„ๅ…ƒ็ด ใ€‚ ่ชžๆณ•: A B็ฏ„ไพ‹: div span ๅฅ—็”จๆ‰€ๆœ‰ <div> ๅ…ƒ็ด ๅ…ง้ƒจ็š„ๆ‰€ๆœ‰ <span> ๅ…ƒ็ด ใ€‚

ๅญไปฃ้ธๆ“‡ๅ™จ๏ผˆChild combinator๏ผ‰

> ็”จไปฅ้ธๆ“‡ๆŸๅ€‹ๅ…ƒ็ด ๅพŒไปฃ็š„ๅ…ƒ็ด ใ€‚ ่ชžๆณ•: A > B๏ผˆB ๅ…ƒ็ด ไธๅฏๅœจ A ๅ…ƒ็ด ็š„ๅ…ถไป–ๅ…ƒ็ด ่ฃก๏ผ‰ ็ฏ„ไพ‹: ul > li ๅฅ—็”จๆ‰€ๆœ‰ <li> ๅ…ƒ็ด ๅ…ง้ƒจ็š„ <ul> ๅญๅ…ƒ็ด ใ€‚

ไธ€่ˆฌๅ…„ๅผŸ้ธๆ“‡ๅ™จ๏ผˆGeneral sibling combinator๏ผ‰

~ combinator selects siblings. This means that the second element follows the first (though not necessarily immediately), and both share the same parent. ่ชžๆณ•: A ~ B ็ฏ„ไพ‹: p ~ span will match all <span> elements that follow a <p>, immediately or not.

็›ธ้„ฐๅ…„ๅผŸ้ธๆ“‡ๅ™จ๏ผˆAdjacent sibling combinator๏ผ‰

+ ้ธๆ“‡็ทŠๆŽฅๅœจๅพŒ็š„ๅ…ƒ็ด ๏ผŒไธฆๅ…ฑไบซ็ˆถๅ…ƒ็ด ใ€‚ ่ชžๆณ•: A + B ็ฏ„ไพ‹: h2 + p ๅฅ—็”จๆ‰€ๆœ‰ ็ทŠๆŽฅๅœจ <h2> ๅ…ƒ็ด ๅพŒ็š„ <p> ๅ…ƒ็ด ๏ผŒไธฆๆ“ๆœ‰ <h2> ็š„็ˆถๅ…ƒ็ด ใ€‚

Column combinator ๅฏฆ้ฉ—ๆ€ง่ณช

The || combinator selects nodes which belong to a column. ่ชžๆณ•: A || B ็ฏ„ไพ‹: col || td will match all <td> elements that belong to the scope of the <col>.

ๅฝ้ธๆ“‡ๅ™จ

Pseudo classes

The : pseudo allow the selection of elements based on state information that is not contained in the document tree. ็ฏ„ไพ‹: a:visited will match all <a> elements that have been visited by the user.

Pseudo elements

The :: pseudo represent entities that are not included in HTML. ็ฏ„ไพ‹: p::first-line will match the first line of all <p> elements.

่ฆ็ฏ„

Specification
Selectors Level 4

See the pseudo-class and pseudo-element specification tables for details on those.

ๅƒ่ฆ‹