jQuery删除每个触发器上的当前活动类

jQuery删除每个触发器上的当前活动类,jquery,Jquery,我编写了一个jQuery函数,它从一个选择框中读取所选的选项值,并将一个活动类添加到与该值对应的div中 jQuery(".product-customize select").on("change", function() { var activeLocation = jQuery(this).find('option:selected').text().toLowerCase().replace(/ /, ''); var dot = '.'; var activeL

我编写了一个jQuery函数,它从一个选择框中读取所选的选项值,并将一个活动类添加到与该值对应的div中

jQuery(".product-customize select").on("change", function() {
    var activeLocation = jQuery(this).find('option:selected').text().toLowerCase().replace(/ /, '');
    var dot = '.';
    var activeLocationDiv = jQuery('.locations').find(dot + activeLocation);
    activeLocationDiv.addClass('active');
});

这是可行的,但我试图做到,每次触发选择框时,当前活动类都将被删除,这样您始终只有一个活动div。

您可以继续这样链接代码,以从其他
选项
同级中删除类
活动

activeLocationDiv.addClass('active').find('option:selected').siglings('option').removeClass('active');
最终代码如下所示:

jQuery(".product-customize select").on("change", function() {
    var activeLocation = jQuery(this).find('option:selected').text().toLowerCase().replace(/ /, '');
    var dot = '.';
    var activeLocationDiv = jQuery('.locations').find(dot + activeLocation);
       activeLocationDiv.addClass('active').find('option:selected').siglings('option').removeClass('active');
});

你的问题在这里解决了 使用.removeclass(“”);在触发之前。。如下所示

  jQuery(".product-customize select").on("change", function() {

   var activeLocation = jQuery(this).find('option:selected').text().toLowerCase().replace(/ /, '');
   var dot = '.';
  var activeLocationDiv = jQuery('.locations').find(dot + activeLocation);
    $('.active').removeClass('active');// Place it here
  activeLocationDiv.addClass('active');
  });
  jQuery(".product-customize select").on("change", function() {

   var activeLocation = jQuery(this).find('option:selected').text().toLowerCase().replace(/ /, '');
   var dot = '.';
  var activeLocationDiv = jQuery('.locations').find(dot + activeLocation);
    $('.active').removeClass('active');// Place it here
  activeLocationDiv.addClass('active');
  });