我有包含ID的HTML行,例如
1
2
3
4
当我用jQuery删除ID 2时,我希望ID 3应设置为2,如
1
2
3
答案 0 :(得分:0)
您可以使用$("#3").attr("id","2");
如果您使用eq()
选择器选择表格行,那将是一个更好的意识形态。例如,只需在表格中使用id="tableid"
,然后选择第一个tr:
//Select the first tr
$('#tableid tr:eq(0)')
//Select the second tr
$('#tableid tr:eq(1)')
//Select the third tr
$('#tableid tr:eq(2)')
//an so on
答案 1 :(得分:0)
您好我在下面创建了一个演示代码,请参阅下面的代码
HTML
<div class="remTable">
<table style="width:50%; border:1px solid red; float:left" >
<tr id="1"><td>1</td></tr>
<tr id="2"><td>2</td></tr>
<tr id="3"><td>3</td></tr>
<tr id="4"><td>4</td></tr>
</table>
<input type="button" name="click" class="clickhere" value="click"/>
</div>
<强>的jQuery 强>
$(".clickhere").click(function(){
var count = $("table tr").size();
$("table tr:eq("+1+")").remove();
var i=1 // note this is the removed tr index or id
for (j=i; j<count; j++)
{
$("table tr:eq("+(j)+")").attr("id", j+1);
}
});
注意:我只更改了tr的id而不是td中的数据,所以当你执行代码时,你会看到
1
3
4
但是tr的id将是
1
2
3
答案 2 :(得分:0)
您的代码中几乎没有错误,请尝试使用此代码
$(".clickhere").click(function(){
$("table tr:eq("+1+")").remove();
var i=0 // note this is the removed tr index or id
var count = $("table tr").size();
for (j=i; j<count; j++)
{
$("table tr:eq("+(j)+")").attr("id", j+1);
$("table tr:eq("+(j)+")").find("td").text(j+1);
}
});
现场演示在js fiddle
尝试此操作