css3之新增选择器-爱代码爱编程
5.新增选择器:
选择器 例子 描述
element>element div>p 选择父元素为<div> 元素的所有 <p> 元素。
element+element div+p 选择紧接在<div> 元素之后的所有 <p> 元素。
属性选择器:
[attribute] [target] 选择带有target 属性所有元素。
[attribute=value] a[target=_back] 选择target="_blank" 的所有元素。
[attribute~=value] [title~=main] 选择title 属性包含单词 "flower" 的所有元素。
[attribute^=value] a[class^="test"] 选择其a属性值以 "test" 开头的每个 <a> 元素。
[attribute$=value] a[class$="text"] 选择其a属性以 "text" 结尾的所有 <a> 元素。
[attribute*=value] a[src*="abc"] 选择其src 属性中包含 "abc" 子串的每个 <a> 元素。
结构:
:first-letter p:first-letter 选择每个<p> 元素的首字母。
:first-line p:first-line 选择每个<p> 元素的首行。
:first-child p:first-child 选择属于父元素的第一个子元素的每个<p> 元素。
:first-of-type p:first-of-type 选择属于其父元素的首个<p> 元素的每个 <p> 元素。
:last-of-type p:last-of-type 选择属于其父元素的最后<p> 元素的每个 <p> 元素。
:only-of-type p:only-of-type选择属于其父元素唯一的<p> 元素的每个 <p> 元素。
:only-child p:only-child 选择属于其父元素的唯一子元素的每个<p> 元素。
父元素有且只有一个子元素
:nth-child(n) p:nth-child(2) 选择属于其父元素的第二个子元素的每个<p> 元素。
odd 奇数行
even 偶数行
n+5==是后5个选中
-n+5==前五个选中
2n 偶数行
2n+1 奇数行
:nth-last-child(n) p:nth-last-child(2) 同上,从最后一个子元素开始计数。
:nth-of-type(n) p:nth-of-type(2)选择属于其父元素第二个<p> 元素的每个 <p> 元素。
:nth-last-of-type(n) p:nth-last-of-type(2) 同上,但是从最后一个子元素开始计数。
:last-child p:last-child选择属于其父元素最后一个子元素每个<p> 元素。
选择器中 带有of-type写法和没有of-type写法的选择器的区别:
带有of-type写法的选择器,具有针对性
没有of-type写法的选择器,不具有针对性
:before p:before 在每个<p> 元素的内容之前插入内容。
:after p:after 在每个<p> 元素的内容之后插入内容。
:focus input:focus 选择获得焦点的input 元素。