jQuery nth-child add html after [英] jQuery nth-child add html after

查看:80
本文介绍了jQuery nth-child add html after的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我该怎么做?以下代码:

 < ul> 
< li>一些内容< / li>
< li>一些内容< / li>
< li>一些内容< / li>
< li>一些内容< / li>

< li class =clear>这是我要添加的< / li>

< li>一些内容< / li>
< li>一些内容< / li>
< li>一些内容< / li>
< li>一些内容< / li>

< li class =clear>这是我要添加的< / li>

等等...
< / ul>

解决方案

使用:nth-子选择器(文档):

  $('ul li:nth-​​child(4n)')。after('< li class =clear>我要添加< / li>')


I have a simple unordered list with 16 list items. I want to add a new list item after every fourth existing list item using jQuery

How would I do that? Code below:

<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>

解决方案

Using the :nth-child selector (documentation):

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

这篇关于jQuery nth-child add html after的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆