我有以下HTML。它的表可以包含很多行id = rows_x。
<div class="row_table" id="row_1">
<div class="row_table" id="row_2">
<div class="row_table" id="row_3">
<div class="row_table" id="row_4">
然后我有id按钮,一旦点击将删除id =“row_2”
$("#button").click(function(){
$('#row_2').remove();
/* Rename rows id goes here */
}
现在我的问题是:
由于row_2已被删除,我需要能够重命名所有后续行。
row_3应该成为row_2等...
答案 0 :(得分:4)
我建议:
$("#button").click(function(){
$('#row_2').remove();
// iterates through each of the elements matched by the selector
$('.row_table').each(
// i represents the index of each of the elements
function(i){
// sets the id of each of the returned elements
// to the string concatenated with the expression 'i + 1'
this.id = 'row_' + (i+1);
});
});