说我有一张桌子:
<table id="mytable">
<tr class="old_row"><td>1</td><td>2</td><td class="edit">Edit</td></tr>
<tr class="old_row"><td>1</td><td>2</td><td class="edit">Edit</td></tr>
<tr class="old_row"><td>1</td><td>2</td><td class="edit">Edit</td></tr>
</table>
我想点击<td>Edit</td>
单元格并使用jquery将整行替换为包含更多内容的新行,例如
$(document).ready(function() {
$('#mytable .edit').click( function() {
var tr = $(this).parent();
var new_row = '<tr class="new_row"><td>3</td><td>4</td><td>Save</td></tr>'
// code to replace this row with the new_row
});
} );
知道如何做到这一点?
答案 0 :(得分:19)
$(document).ready(function() {
$('#mytable .edit').click( function() {
var new_row = '<tr class="new_row"><td>3</td><td>4</td><td>Save</td></tr>'
$(this).parent().replaceWith(new_row);
});
} );
答案 1 :(得分:4)
$(document).ready(function() {
$('#mytable .edit').click( function() {
var tr = $(this).parent();
var new_row = '<tr class="new_row"><td>3</td><td>4</td><td>Save</td></tr>';
tr.replaceWith(new_row);
});
});
答案 2 :(得分:1)
jQuery的replaceWith()
。例如:
$(document).ready(function() {
$('#mytable .edit').click( function() {
var tr = $(this).parent();
var new_row = '<tr class="new_row"><td>3</td><td>4</td><td>Save</td></tr>'
tr.replaceWith(new_row); // code to replace this row with the new_row
});
} );
答案 3 :(得分:0)
$('.edit').click(function(){
$(this).parent().removeClass('old_row').addClass('new_row').html('<td>3</td><td>4</td><td>Save</td>');
});