语法 | 描述 |
---|---|
B:first | 匹配页面上满足选择器B的第一个元素 |
B:last | 匹配页面上满足选择器B的最后一个元素 |
B:first-child | 所有匹配选择器B的第一个元素 |
B:last-child | 所有匹配选择器B的所有最后一个元素 |
B:only-child | 匹配选择器B的唯一元素。 |
B:nth-child(n) | 匹配选择器B的第N个子元素,从1开始。 |
B:nth-child(odd,even) | 匹配选择器B的奇数位,偶数位子元素 |
B:nth-child(Xn+Y) | 匹配选择器B的第(Xn+Y)个子元素 |
B:even | 所有偶数位匹配选择B的元素 |
B:odd | 所有奇数位匹配选择B的元素 |
B:eq(n) | 第N个匹配选择B的元素,从0开始 |
B:gt(n) | 第N个元素之后匹配选择B的元素,从0开始 |
B:lt(n) | 第N个元素之前匹配选择B的元素,从0开始 |
示例:
语句 | 说明 |
---|---|
$(“p:first”) | selects the first element on the page |
$(img[src§=.png]:first”) | selects the first <img> element on the page that has a src attribute ending in .png |
$(“button.small:last”) | selects the last <button> element on the page that has a class name of small |
$(“li:first-child”) | selects all <li> elements that are first children within their lists |
$(“a:only-child”) | selects all <a> elements that are the only element within their parent |
$(“li:nth-child(2)”) | selects all <li> elements that are the second item within their lists |
$(“tr:nth-child(odd)”) | selects all odd <tr> elements within a table |
$(“div:nth-child(5n)”) | selects every 5th <div> element |
$(“div:nth-child(5n+1)”) | selects the element after every 5th <div> element |
$(“.someClass:eq(1)”) | selects the second element with a class name of someClass |
$(“.someClass:gt(1)”) | selects all but the first two elements with a class name of someClass |
$(“.someClass:lt(4)”) | selects the first four elements with a class name of someClass |