【问题标题】:jQuery nth-child add html afterjQuery nth-child 后添加 html
【发布时间】:2012-11-16 13:06:11
【问题描述】:

我有一个包含 16 个列表项的简单无序列表。我想使用 jQuery 在每四个现有列表项之后添加一个新列表项

我该怎么做?代码如下:

<ul>
    <li>some stuff</li>
    <li>some stuff</li>
    <li>some stuff</li>
    <li>some stuff</li>

    <li class="clear">This is what I want to add</li>

    <li>some stuff</li>
    <li>some stuff</li>
    <li>some stuff</li>
    <li>some stuff</li>

    <li class="clear">This is what I want to add</li>

    and so on...
</ul>

【问题讨论】:

标签: jquery css-selectors


【解决方案1】:

使用:nth-child 选择器 (documentation):

$('ul li:nth-child(4n)').after('<li class="clear">This is what I want to add</li>')

【讨论】:

  • 我是这样使用的:$('ul li:nth-child(1n)').after('
  • ');但是如果我在最后一个 LI 之后不需要 .sep 类 LI 怎么办?
  • 最好问一个新问题,@TheHun。一个简单的方法是在那之后删除不需要的li。也许使用.not() 也可以。
  • 猜你喜欢
    相关资源
    最近更新 更多
    热门标签