我想寫下我的表的第一列上每行的數量。 我有幾個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 });
它返回的相同。 謝謝您的閱讀
我不能讓你.....數組已經是排序格式'[1,2,3 ...] ' –
您是否在瀏覽器控制檯中檢查了輸出?通過在每個循環中像下面這樣檢查索引:'$('。studentCounter')。each(function(i){console.log(i); $(this).html(i + 1); }) ' –
是的,我有。相同的結果。數字如:1,10,2,... –