2011-07-16 7 views
0

私は次のHTMLを持っています。そのテーブルには、多くの行id = rows_xを含むことができます。 row_2が削除されているのでjQueryのクラス名からの名前変更

私がする必要がある:

<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 = "row_2"

$("#button").click(function(){ 
    $('#row_2').remove(); 
    /* Rename rows id goes here */ 
} 

を削除しますクリックするとIDボタンを持っています次のすべての行の名前を変更することができます。

row_3はなど、row_2なるはず...

+0

私はなぜこれを行うのですか? (そのIDの目的は何ですか?) – meo

答えて

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); 
      }); 
}); 

JS Fiddleを。

+0

3行目と4行目は2行目と3行目になるはずです – Yannick

+0

[JS Fiddle](http://jsfiddle.net/davidThomas/tA2hm/)を見ましたか?彼らはそうする。行のテキストコンテンツも変更したいのでないかぎり? –

+0

変更されたIDを示すために、[JS Fiddle](http://jsfiddle.net/davidThomas/tA2hm/1/)を更新しました。 –

関連する問題