':eq(index)' 只匹配一个元素,而这个将为每一个父元素匹配子元素。:nth-child从1开始的,而:eq()是从0算起的! 可以使用:
nth-child(even)
:nth-child(odd)
:nth-child(3n)
:nth-child(2)
:nth-child(3n+1)
:nth-child(3n+2)
matches the nth-child of its parent.
while ':eq(index)' matches only a single element, this matches more then one: one for each parent. the specified index is one-indexed, in contrast to :eq() which starst at zero.
返回值array
参数index (number) : 要匹配元素的序号,从1开始
示例在每个 ul 查找第 2 个li
html 代码:
john
karl
brandon
glen
tane
ralph
jquery 代码:
$(ul li:nth-child(2))
结果:
[ karl, tane ]
--------------------------------------------------------------------------------
:first-child匹配第一个子元素
':first' 只匹配一个元素,而此选择符将为每个父元素匹配一个子元素
matches the first child of its parent.
while ':first' matches only a single element, this matches more then one: one for each parent.
返回值array
示例在每个 ul 中查找第一个 li
html 代码:
john
karl
brandon
glen
tane
ralph
jquery 代码:
$(ul li:first-child)
结果:
[ john, glen ]
--------------------------------------------------------------------------------
:last-child匹配最后一个子元素
':last'只匹配一个元素,而此选择符将为每个父元素匹配一个子元素
matches the last child of its parent.
while ':last' matches only a single element, this matches more then one: one for each parent.
返回值array
示例在每个 ul 中查找最后一个 li
html 代码:
john
karl
brandon
glen
tane
ralph
jquery 代码:
$(ul li:last-child)
结果:
[ brandon, ralph ]
--------------------------------------------------------------------------------
:only-child如果某个元素是父元素中唯一的子元素,那将会被匹配
如果父元素中含有其他元素,那将不会被匹配。
matches the only child of its parent.
if the parent has other child elements, nothing is matched.
返回值array
示例在 ul 中查找是唯一子元素的 li
html 代码:
john
karl
brandon
glen
jquery 代码:
$(ul li:only-child)
结果:
[ glen ]
--------------------------------------------------------------------------------
:input匹配所有 input, textarea, select 和 button 元素
matches all input, textarea, select and button elements.
返回值array
示例查找所有的input元素
html 代码:
jquery 代码:
$(:input)
结果:
[ , , , , , , , , ]
--------------------------------------------------------------------------------
:text匹配所有的单行文本框
matches all input elements of type text.
返回值array
示例查找所有文本框
html 代码:
jquery 代码:
$(:text)
结果:
[ ]
--------------------------------------------------------------------------------
:password匹配所有密码框
matches all input elements of type password.
返回值array
示例查找所有密码框
html 代码:
jquery 代码:
$(:password)
结果:
[ ]
--------------------------------------------------------------------------------
:radio匹配所有单选按钮
matches all input elements of type radio.
返回值array
示例查找所有单选按钮
html 代码:
jquery 代码:
$(:radio)
结果:
[ ]
--------------------------------------------------------------------------------
:submit匹配所有提交按钮
matches all input elements of type submit.
返回值array
示例查找所有提交按钮
html 代码:
jquery 代码:
$(:submit)
结果:
[ ]
其他的一些 都是一样的道理:image ,:reset,:button,:file,:hidden !@#@!%$%
