0
$('.toggle').addClass('change_button').removeClass('toggle');
$(this).removeClass('change_button').addClass('toggle');


i have provided the demo of complete code below in the first comment.
i have 2 sets of buttons

1st set
1.Lite
2.Medium
3.Heavy

Demo: Fiddle


2nd set
1.Yes
2.No

what i want is those sets must act as radio buttons like demo provided below in the first comment
instead of radio buttons i have here block buttons and when the button is selected i want to change bagraund color

4

1 回答 1

2

尝试这个:-

演示

  $('.change_button.toggle',$(this).closest('div')).removeClass('toggle');
  $(this).addClass('toggle');

 });

解释:-

$('.change_button.toggle',$(this).closest('div')).removeClass('toggle'); 

此语句选择位于单击元素的父 div 中的具有 .change_button 和 .toggle 类的元素并删除其toggle类。

$(this).addClass('toggle'); 

将类添加到选定元素。

于 2013-04-30T16:39:14.547 回答