使用jQuery将自动增量值添加到li元素

时间:2015-07-31 21:36:40

标签: php jquery html-lists element auto-increment

我有以下代码,它是由插件动态生成的,该插件将为每个团队成员创建一个“li”元素,目前我有150个成员,因此需要添加一个自动增量ID。谢谢!

<ul id="dvteamgrid<?php echo esc_attr($random); ?>" class="dvteamgrid withanim">
<li data-filter-class='["gridall",<?php echo $filters; ?>]'>
    <img src="<?php echo esc_url($thumb_url); ?>" alt="<?php the_title(); ?>"/>
</li>

这就是html代码的样子:

<ul id="dvteamgrid225306813" class="dvteamgrid withanim">
  <li data-filter-class="["gridall","dvfilter5997", "dvfilter6007", "dvfilter6025";]">
    <img src="/wp-content/uploads/2015/03/Abee-C.jpg">
  </li>
</ul>

1 个答案:

答案 0 :(得分:1)

您需要遍历每个li元素并附加自动增加的ID。

var auto_inc_id = 0; //initialise value of auto incremented id to 0
$('ul.dvteamgrid li').each(function(){ //loop through each list element of ul
    $(this).attr('id', auto_inc_id); //attach to current li an id attribute that has auto_inc_id as its value
    auto_inc_id++; //increment auto_inc_id by one
});