使用Jquery计算和枚举LI

时间:2013-04-14 15:00:43

标签: jquery

我有这个UI和一些由Wordpress生成的Li。 如果我使用<?php the_ID(); ?>它会搞得一团糟,那么帖子ID就不合适了。 我需要做的是让jquery计数并在我的简单列表中写下1,2,3和4号 这是我的UI 该循环将获得4个帖子,因此,4 <li>'s

<div id="controle">  
    <ul>
<?php
while ( $loop->have_posts() ) : $loop->the_post();
?>

  <li>
<a href="<?php the_ID(); ?>">1 <--! see? here is where the count goes :D --> </a>
  </li>

<?php endwhile; ?>
    </ul>
        </div>

我想到了

$('#controle li').each(function(e){

}

会做到这一点,但不知道如何继续这样做:(

谢谢大家!

2 个答案:

答案 0 :(得分:0)

试试这个:

$( "#controle li" ).each(function( index ) {
  $(this).find("a").text(index + 1);
});

DEMO HERE

答案 1 :(得分:0)

使用each循环,each回调的第一个参数为index

$('#controle li a').each(function(index){
    $(this).prepend('<span class="myNumberClass">'+ (index+1) +'</span>')
})
相关问题