Jquery 一次仅启用两个切换
我有6个过滤器和jQuery功能,可以在单击它们时切换“开”类 如何对功能进行编码,以便一次只能“打开”两个按钮Jquery 一次仅启用两个切换,jquery,Jquery,我有6个过滤器和jQuery功能,可以在单击它们时切换“开”类 如何对功能进行编码,以便一次只能“打开”两个按钮 例如,如果单击1,然后单击3,它们都将处于“打开”状态。但是,如果单击4,最旧的按钮(1)将关闭,然后4将打开。您必须跟踪切换过滤器的顺序 var filters = []; var filters_max_on= 3; $('.filter').click(function(){ //turn the filter on $(this).addClass('on');
例如,如果单击1,然后单击3,它们都将处于“打开”状态。但是,如果单击4,最旧的按钮(1)将关闭,然后4将打开。您必须跟踪切换过滤器的顺序
var filters = [];
var filters_max_on= 3;
$('.filter').click(function(){
//turn the filter on
$(this).addClass('on');
//add it to the front of the array
filters.unshift($(this));
//check if there are too many filters active
if(filters.length > filters_max_on){
//remove the filter from the list and turn it off
filters.pop().removeClass('on');
}
});
.切换{颜色:绿色;}
.toggle a.on{color:red;}
$(函数(){
所选变量=[null,null];
$('.toggle a')。单击(函数(){
如果(选中[0]!=null)
已选择[0]。removeClass('on');
$(this.addClass('on');
已选[0]=已选[1];
所选[1]=$(此项);
});
});
如果您能发布您尝试过的内容,那就更好了。一定要读这个
方法是:1.将按钮的id保存在数组中
2.检查是否选择了两个以上的选项
3.单击“更新数组”并更改类
var onButtons = [];
$('button').on('click',function(){
$(this).removeClass('off').addClass('on');
if(onButtons.length == 2){
$('#'+onButtons[0]).removeClass('on').addClass('off');
onButtons.splice(0, 1);
}
onButtons.push($(this).attr('id'));
});
有什么可以给我们看的吗?你的HTML标记和/或javascript?请看,其中的共识是“不,他们不应该”!
var onButtons = [];
$('button').on('click',function(){
$(this).removeClass('off').addClass('on');
if(onButtons.length == 2){
$('#'+onButtons[0]).removeClass('on').addClass('off');
onButtons.splice(0, 1);
}
onButtons.push($(this).attr('id'));
});