使用Jquery函数删除css类的所有实例

时间:2013-11-04 04:08:23

标签: javascript jquery css

我有全局变量:

          units = 0;
          majorUnits = 0; 
          supportUnits = 0;
          requiredUnits = 0;
          electiveUnits = 0;
          electiveUnitsStop = 0;
          pending = 0;
          percent = 0;
          supportPercent = 0;
          requiredPercent = 0;
          electivePercent = 0;

我在函数上使用这些变量:

      $("tr").click(function() {
        $(this).toggleClass("taken");
        if ( $(this).hasClass('taken') ) 
        { .....

然后我有第二个函数,我将变量设置为零:

      $( "#clear-button" ).click(function() {
          units = 0;
          majorUnits = 0; 
          supportUnits = 0;
          requiredUnits = 0;
          electiveUnits = 0;
          electiveUnitsStop = 0;
          pending = 0;
          percent = 0;
          supportPercent = 0;
          requiredPercent = 0;
          electivePercent = 0;
          // $( "tr" ).click(); 
          //$("tr").find('taken').removeClass('taken');
          //$("table").closest('tr').find('.taken').removeClass('taken');
          //$("tr").removeClass();
          //taken.allInstances = [];
        });

如上所述,注释代码中我试图删除所有出现的内容 类taken,可能存在于我的html表的行中,也可能不存在。

我怎么能做到这一点?!?!

我真的很感激任何帮助。

1 个答案:

答案 0 :(得分:0)

您是要删除还是元素

从子元素中删除类:

$('table#tablename .taken').removeClass('taken');

http://api.jquery.com/removeClass/

完全删除元素:

$('table#tablename .taken').remove();

http://api.jquery.com/remove/

此外,根据您的HTML结构,您可以更具体地使用目标并使用this而不是指定表名(也使用findclosest等)