2017-02-13 81 views
1

我想写下我的表的第一列上每行的数量。 我有几个DOM元素与指定的类studentCounter排序索引元素

我尝试以下:

$('.studentCounter').each(function (i) { 
    $(this).html(i + 1); 
}) 

然而它返回号码,如:1,10,2,3 ..等。

然后我排序的元素的索引是这样的:

var studentItems = []; 

$('.studentCounter').each(function (i) { 
    studentItems.push(i + 1); 
}) 

studentItems.sort(function(a, b) { return a - b }); 

它返回的相同。 谢谢您的阅读

+0

我不能让你.....数组已经是排序格式'[1,2,3 ...] ' –

+0

您是否在浏览器控制台中检查了输出?通过在每个循环中像下面这样检查索引:'$('。studentCounter')。each(function(i){console.log(i); $(this).html(i + 1); }) ' –

+0

是的,我有。相同的结果。数字如:1,10,2,... –

回答

0

如果我理解正确,您希望每行都有编号。如果是这样,请看Snippet。 详细信息评论在片段中。

注:作为例子就是现在,如果你有一个以上的<tbody><table>将继续不断号码的第一列。如果这不符合要求的行为,您必须稍微改变选择器。除非你提供更多的HTML,否则我无法告诉你。

代码片段

/* The selector is: 
 
|| Find the first <td> of every <tr> of every <tbody> 
 
|| each() <td> found under that criterion... 
 
|| ~~(referenced as $(this) on second line)~~ 
 
|| ...set it's text content to it's index number (var idx) 
 
|| offset by 1 (+1). 
 
*/ 
 
$('tbody tr td:first-of-type').each(function(idx, td) { 
 
    $(this).text(idx + 1); 
 
});
table { 
 
    border: 1px solid black; 
 
} 
 
td, 
 
th { 
 
    border: 1px dashed red; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<table> 
 
    <thead> 
 
    <tr> 
 
     <th>Row</th> 
 
     <th>Col 1</th> 
 
     <th>Col 2</th> 
 
    </tr> 
 
    </thead> 
 
    <tbody> 
 
    <tr> 
 
     <td></td> 
 
     <td>&nbsp;</td> 
 
     <td>&nbsp;</td> 
 
    </tr> 
 
    <tr> 
 
     <td></td> 
 
     <td>&nbsp;</td> 
 
     <td>&nbsp;</td> 
 
    </tr> 
 
    <tr> 
 
     <td></td> 
 
     <td>&nbsp;</td> 
 
     <td>&nbsp;</td> 
 
    </tr> 
 
    <tr> 
 
     <td></td> 
 
     <td>&nbsp;</td> 
 
     <td>&nbsp;</td> 
 
    </tr> 
 
    <tr> 
 
     <td></td> 
 
     <td>&nbsp;</td> 
 
     <td>&nbsp;</td> 
 
    </tr> 
 
    <tr> 
 
     <td></td> 
 
     <td>&nbsp;</td> 
 
     <td>&nbsp;</td> 
 
    </tr> 
 
    <tr> 
 
     <td></td> 
 
     <td>&nbsp;</td> 
 
     <td>&nbsp;</td> 
 
    </tr> 
 
    <tr> 
 
     <td></td> 
 
     <td>&nbsp;</td> 
 
     <td>&nbsp;</td> 
 
    </tr> 
 
    <tr> 
 
     <td></td> 
 
     <td>&nbsp;</td> 
 
     <td>&nbsp;</td> 
 
    </tr> 
 
    <tr> 
 
     <td></td> 
 
     <td>&nbsp;</td> 
 
     <td>&nbsp;</td> 
 
    </tr> 
 
    <tr> 
 
     <td></td> 
 
     <td>&nbsp;</td> 
 
     <td>&nbsp;</td> 
 
    </tr> 
 
    <tr> 
 
     <td></td> 
 
     <td>&nbsp;</td> 
 
     <td>&nbsp;</td> 
 
    </tr> 
 
    <tr> 
 
     <td></td> 
 
     <td>&nbsp;</td> 
 
     <td>&nbsp;</td> 
 
    </tr> 
 
    <tr> 
 
     <td></td> 
 
     <td>&nbsp;</td> 
 
     <td>&nbsp;</td> 
 
    </tr> 
 
    </tbody> 
 
</table> 
 
<table> 
 
    <thead> 
 
    <tr> 
 
     <th>Row</th> 
 
     <th>Col 1</th> 
 
     <th>Col 2</th> 
 
    </tr> 
 
    </thead> 
 
    <tbody> 
 
    <tr> 
 
     <td></td> 
 
     <td>&nbsp;</td> 
 
     <td>&nbsp;</td> 
 
    </tr> 
 
    <tr> 
 
     <td></td> 
 
     <td>&nbsp;</td> 
 
     <td>&nbsp;</td> 
 
    </tr> 
 
    <tr> 
 
     <td></td> 
 
     <td>&nbsp;</td> 
 
     <td>&nbsp;</td> 
 
    </tr> 
 
    <tr> 
 
     <td></td> 
 
     <td>&nbsp;</td> 
 
     <td>&nbsp;</td> 
 
    </tr> 
 
    <tr> 
 
     <td></td> 
 
     <td>&nbsp;</td> 
 
     <td>&nbsp;</td> 
 
    </tr> 
 
    <tr> 
 
     <td></td> 
 
     <td>&nbsp;</td> 
 
     <td>&nbsp;</td> 
 
    </tr> 
 
    <tr> 
 
     <td></td> 
 
     <td>&nbsp;</td> 
 
     <td>&nbsp;</td> 
 
    </tr> 
 
    <tr> 
 
     <td></td> 
 
     <td>&nbsp;</td> 
 
     <td>&nbsp;</td> 
 
    </tr> 
 
    <tr> 
 
     <td></td> 
 
     <td>&nbsp;</td> 
 
     <td>&nbsp;</td> 
 
    </tr> 
 
    <tr> 
 
     <td></td> 
 
     <td>&nbsp;</td> 
 
     <td>&nbsp;</td> 
 
    </tr> 
 
    <tr> 
 
     <td></td> 
 
     <td>&nbsp;</td> 
 
     <td>&nbsp;</td> 
 
    </tr> 
 
    <tr> 
 
     <td></td> 
 
     <td>&nbsp;</td> 
 
     <td>&nbsp;</td> 
 
    </tr> 
 
    <tr> 
 
     <td></td> 
 
     <td>&nbsp;</td> 
 
     <td>&nbsp;</td> 
 
    </tr> 
 
    <tr> 
 
     <td></td> 
 
     <td>&nbsp;</td> 
 
     <td>&nbsp;</td> 
 
    </tr> 
 
    </tbody> 
 
</table>

+0

非常感谢你。你的代码工作完美。但在我的案例 我有我的网页sevaral表,因此它replcaces在第一列表中的所有数据。这就是为什么我在td中创建了span类'studentCounter',所以我只需要在span类中写下行计数器 –