$("ul li:nth-child(2)").append(" - 2nd!");nth-child(2)什么意思???

plglenn15 2009-11-15 11:18:27
nth-child(2)什么意思???

http://docs.jquery.com/Selectors/nthChild#index
Selectors/nthChild
From jQuery JavaScript Library
Jump to: navigation, search

« Back to Selectors
[edit]
:nth-child(index/even/odd/equation)

* Overview
* Examples

Matches all elements that are the nth-child of their parent or that are the parent's even or odd children.
While :eq(index) matches only a single element, this matches more than one: One for each parent with index. Multiple for each parent with even, odd, or equation. The specified index is one-indexed, in contrast to :eq() which starts at zero.

* Demo
* View Source

Finds the second li in each matched ul and notes it.

$("ul li:nth-child(2)").append("<span> - 2nd!</span>");

...全文
238 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
chen_ya_ping 2009-11-15
  • 打赏
  • 举报
回复
建议楼主下载一份jquery的文档,上面有很详细的解释。或者网上还有电子书的。这个是代表的这个节点下的第2个子节点,那个n是从1开始的。其他的地方都是从0开始。
qqzeng-ip 2009-11-15
  • 打赏
  • 举报
回复
<ul>
<li>John</li>
<li>Karl</li>
<li>Brandon</li>
</ul>
<ul>
<li>Glen</li>
<li>Tane</li>
<li>Ralph</li>
</ul>


$("ul li:nth-child(2)")

结果:
<li>Karl</li>,   <li>Tane</li> 
wuyq11 2009-11-15
  • 打赏
  • 举报
回复
nth-child(n) 返回第n个子元素. 如:li:nth-child(2) 返回 所有作为子元素的 li 项的第二个.
:nth-child(even|odd) 返回偶数(Even) 或奇数(Odd) 子元素. 如:li:nth-child(even) 返回所有作为子元素的li项的偶数元素.
:nth-child(Xn+Y) 返回Xn+Y计算结果的第n个子元素.如果Y为0,则被忽略. 如:li:nth-child(3n) 返回所有作为子元素的li项的每第3n个元素
qqzeng-ip 2009-11-15
  • 打赏
  • 举报
回复
在每个 ul 查找第 2 个li

87,994

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧