jquery removeClass 不删除类

问题描述 投票:0回答:1

在我的数据表的createdRow函数中,我试图从最后一个TD中删除一个类。下面是该函数的代码。添加类只是一个测试,看看该函数是否被触发,这似乎是因为“测试”类被添加到最后一个 TD 中。但删除类函数不会删除该类。我似乎无法弄清楚,我知道我可以(也许)在创建类后循环它,但我想使用 DataTables 的本机函数,这确实让我烦恼,所以我想尝试弄清楚它。

createdRow: function(row,data,dataIndex){
 console.log(jquery(row).find("td:last")); //prints the last td in each row
 jquery(row).find("td:last").addClass("test");//this works the last TD of the table has class test
 jquery(row).find("td:last").removeClass("dt-type-date");// class is still listed in the td
 jquery(row).find("td:last")[0].removeClass("dt-type-date");//tried this as well but this gives me an error that removeClass is not a function. 
}
jquery datatables
1个回答
0
投票

我能够让它工作的一种方法是在等待数据表初始化完成后使用 classList.remove 。

await create_modal(jQuery('#modal_table'),evt.currentTarget.querySelector('.num-description').innerText);

document.querySelectorAll('#modal_table td').forEach((td) => {
    if (td.classList.contains('dt-type-date')){
        td.classList.remove('dt-type-date');
    } 
});
© www.soinside.com 2019 - 2024. All rights reserved.