jQuery从类元素重命名id

jQuery从类元素重命名id,jquery,Jquery,我有下面的html。它是一个可以包含许多行的表,id=rows\u 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"> 现在回答我的问题: 由于第2行已被删除,我需要能够重命名以下所有行 第3行应该变成第2行,等等。我

我有下面的html。它是一个可以包含许多行的表,id=rows\u 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">
现在回答我的问题:

由于第2行已被删除,我需要能够重命名以下所有行

第3行应该变成第2行,等等。

我建议:

$("#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);
          });
});
.

我建议:

$("#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);
          });
});

.

但是第3行和第4行应该变成第2行和第3行。您看到了吗?是的。除非您还想更改行的文本内容?更新了,以演示更改的
id
。嘿,很棒的工作@David。您能解释一下这是如何工作的吗?但是第3行和第4行应该变成第2行和第3行吗?是的。除非您还想更改行的文本内容?更新了,以演示更改的
id
。嘿,干得好@David你能解释一下这是如何工作的吗?我能问一下你为什么这样做吗?(这些身份证的目的是什么?)我能问一下你为什么这么做吗?(这些ID的用途是什么?)