为什么 $(this).attr( "id" ) 在使用 javascript 更改旧属性后获取旧属性



我有一个奇怪的问题,所以在删除一行后更改了所有表行的id
这是我代码的一部分:

$(document).on("click",".fa-trash-alt",function(){
console.log($(this).attr("id"))
let index = parseInt($(this).attr("id"))
globalFunctions.removeFromChoosedList(index)
$('tr#'+index).remove()
let rows = $(".choosed-table-data tr")
$.each(rows,function(index,value){
$(value).attr("id",index)
})
})

问题是,在该操作之后,当我单击应该具有新id的行时,尽管检查员上的id发生了更改,但旧id仍显示为console.log($(this).attr("id"))

很难给出一个例子,因为没有HTML可以将js代码关联起来。如果你想要一个更相关的例子,请编辑问题以包含HTML。

$(".clicked-elm").click((e) => {
// removes the clicked element
e.target.remove();

// if you want to remove the children instead do
e.target.children.forEach(elm => elm.remove());

// not sure what chosen data table is, 
// but this example should change the id's of each element with that class,
// unless $() only picks first element. 
// If so, change it to $$() and it should select all matches.
$(".choosed-table-data tr").forEach((elm, i) => $(elm).attr("id", i));
});

@Invizi谢谢你的澄清,显然你的说明是对的,我认为我的问题很清楚,但事实并非如此。不管怎样,我找到了我犯错误的地方,正如你所看到的,我想得到这一排的";id";(tr(,它是按钮(".trass-alt"(的父级,但我所做的是:$(this).attr("id")我应该在哪里执行此操作:
$(this).parentElement.parentElement.id穿过父母,直到我到达平均值:=>>我觉得我是白痴XD最后是我的固定代码

$(document).on("click",".fa-trash-alt",function(e){
let parent = e.target.parentElement.parentElement
let index = parent.id
globalFunctions.removeFromChoosedList(index)
parent.remove()
let rows = $(".choosed-table-data tr")
$.each(rows,function(index,value){
$(value).attr("id",index)
})
})

最新更新