I wish to duplicate a tr tag in jquery. I have the following. The issue with this code is that
$(rowId)
doesn't include the "tr" but just the contents inside it. So the first tag is a <td>
not a <tr>
. How can select this element also.
$('.addLine').click(fu开发者_如何转开发nction () {
var rowId = $(this).attr('rel');
var rowId = "#" + rowId;
var newRow = $(rowId);
var htmlStr = $(newRow).html();
$(newRow).append(htmlStr);
});
.html()
returns the inside of the element selected, you can use .clone()
to make a copy of the entire element, which you can append.
$('.addLine').click(function () {
var rowId = $(this).attr('rel');
var newRow = $("#"+rowId).clone().appendTo("youTableOrSomething");
});
Try this one
$('.addLine').click(function () {
var rowId = $(this).attr('rel');
var rowId = "#" + rowId;
var newRow = $(rowId).parent();
var htmlStr = $(newRow).html();
$(newRow).append(htmlStr);
});
精彩评论