如何从同一元素中删除同一类

How to remove the same class from the same element?

本文关键字:一类 删除 元素      更新时间:2023-09-26

点击后,我想从位于diffrentdiv:的相同元素中删除相同的类

这是JS代码。

$("#dialog").on('click',".selected_channel:not(.unselectable)", function(){ 
    $(this).removeClass("selected_channel");
    $("#item_container").find($(this)).removeClass("selected_channel");
});

您需要使用类选择器来定位元素

$("#dialog").on('click',".selected_channel:not(.unselectable)", function(){ 
    $(this).removeClass("selected_channel");
    $("#item_container .selected_channel").removeClass("selected_channel");
});

当您说$("#item_container").find($(this))时,只有当它是id为item_container 的元素的后代时,它才会返回this引用的元素