简体   繁体   中英

how to remove class of a div without knowning id of div?

Hi i have a dynamic generated div's list with unique id's for every generated div's,

when click on one of the dynamic generated div this will change the selected div background color by this code $(this).addClass('add_color'); if again i click on another one div then the older selected div background color should change to default so i tried this code $(".add_color").removeClass(".add_color"); but it's not working, please help.

$(".className").removeClass("className")

如果您只需要删除包含该特定类的任何div的类

$("div").removeClass('someClass'); 

You need to modify the click handler to add/remove class on click of divs:

$('body').on('click','.somedivs',function(){
   $(".add_color").not($(this)).removeClass("add_color");
   $(this).addClass('add_color');
});
$(".your_class").removeClass("your_class");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM