1

我创建了一个页面来玩 960 网格系统,了解 div 放置。页面上最多有四个 div。当从下拉列表中选择新的网格大小、拉取或推送值时,我需要删除当前类,即grid_8pull_5push_4并添加新选择的类。

function alter_grid1(new_class_name){

   var old = document.getElementById('grid1');
   var classes = old.className.split(' ');
   var old_class_name = 'cessna';
   $.each(classes, function(){

        if(old_class_name == 'cessna' || old_class_name == null){
          old_class_name = this.match('/grid_1|grid_2|grid_3|grid_4|grid_5|grid_6|grid_7|grid_8|grid_9|grid_10|grid_11|grid_12|grid_13|grid_14|grid_15|grid_16|grid_17/');
       }
   });
   $(old).removeClass('grid_1 grid_2 grid_3 grid_4 grid_5 grid_6 grid_7 grid_8 grid_9 grid_10 grid_11 grid_12 grid_13 grid_14 grid_15 grid_16').addClass(new_class_name);
   console.log('Old grid1 class name is ' + old_class_name + ' and New grid1 class name is ' + new_class_name);
}

我可以addClass(new_class_name);毫无困难地使用。

我可以毫无问题地使用 use removeClass('grid_8');(grid_8 是实际的类名)

当我使用removeClass(old_class_name);它时,它不起作用。没有错误消息,萤火虫中没有任何内容,只是不起作用。

因为我不知道之前的值是什么,所以我必须删除任何可能存在的类

 $(old).removeClass('grid_1 grid_2 grid_3 grid_4 grid_5 grid_6 grid_7 grid_8 grid_9 grid_10 grid_11 grid_12 grid_13 grid_14 grid_15 grid_16').addClass(new_class_name);

这是我让它工作的唯一方法。当然可以,但似乎我要么错过了什么,要么做错了什么。如果有帮助,您可以在这里看到我正在尝试做的事情。我只是不知道如何使用 var 添加一个类,但不能以相同的方式删除一个类。

4

1 回答 1

2

似乎你的正则表达式是错误的。使用不带斜线的字符串或仅使用斜线。

old_class_name = this.match(/grid_1|grid_2|grid_3|grid_4|grid_5|grid_6|grid_7|grid_8|grid_9|grid_10|grid_11|grid_12|grid_13|grid_14|grid_15|grid_16|grid_17/);

甚至可能

old_class_name = this.match(/grid_[0-9]+/);
于 2012-01-31T09:47:57.150 回答