Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何更改div中的按钮类,而最后一个按钮类可以保留更改?_Javascript_Jquery_Html_Css - Fatal编程技术网

Javascript 如何更改div中的按钮类,而最后一个按钮类可以保留更改?

Javascript 如何更改div中的按钮类,而最后一个按钮类可以保留更改?,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我正在使用此代码更改按钮的类: $('button').on('click', function(){ var btn=$(this); if(btn.attr('class')=='tct-button'){ btn.removeClass('tct-button').addClass('tct-button2'); } else{ btn.removeClass('tct-button2').addClass('tct-butt

我正在使用此代码更改按钮的类:

$('button').on('click', function(){
    var btn=$(this);
    if(btn.attr('class')=='tct-button'){
        btn.removeClass('tct-button').addClass('tct-button2');
    }
    else{
        btn.removeClass('tct-button2').addClass('tct-button');
    }

});
问题是我有多个div,每个div都有多个按钮。我需要更改它,以便每次我单击div中的按钮时,其他被上一次单击更改的按钮(在同一个div中)都会更改回默认类,即“tct-button”,并且只有最后单击的按钮变为“tct-button2”。你能帮帮我吗。

使用

确定是否将任何匹配的元素分配给给定的类

代码

但是我认为你需要

$('button').on('click', function(){
    $('.tct-button2').removeClass('tct-button2'); //Remove all class 
    $(this).addClass('tct-button2'); //Add the class to current element
});
使用

确定是否将任何匹配的元素分配给给定的类

代码

但是我认为你需要

$('button').on('click', function(){
    $('.tct-button2').removeClass('tct-button2'); //Remove all class 
    $(this).addClass('tct-button2'); //Add the class to current element
});
使用

确定是否将任何匹配的元素分配给给定的类

代码

但是我认为你需要

$('button').on('click', function(){
    $('.tct-button2').removeClass('tct-button2'); //Remove all class 
    $(this).addClass('tct-button2'); //Add the class to current element
});
使用

确定是否将任何匹配的元素分配给给定的类

代码

但是我认为你需要

$('button').on('click', function(){
    $('.tct-button2').removeClass('tct-button2'); //Remove all class 
    $(this).addClass('tct-button2'); //Add the class to current element
});
在jquery中使用,不需要检查hasClass()

在jquery中使用,不需要检查hasClass()

在jquery中使用,不需要检查hasClass()

在jquery中使用,不需要检查hasClass()


如果您想在按钮(tct-button2)上添加一个附加类,同时在每个按钮上保留tct按钮,您可以按照Satpal的建议执行

但是,从它的声音来看,我可能是错的,如果您想更改类,使每个按钮一次只有一个类(tct按钮或tct-button2),您可以执行与Satpal建议的类似的操作并使用:

$('button').on('click', function(){
    $('.tct-button2').addClass('tct-button').removeClass('tct-button2'); //Add original class and Remove tct-button2 class
    $(this).removeClass('tct-button').addClass('tct-button2'); //Add the class to current element
});
这是一个例子

要仅影响单击按钮所属的div中的按钮,请使用类似的方法:

$('button').on('click', function(){
    $(this).parent('div').find('.tct-button2').addClass('tct-button').removeClass('tct-button2'); //Remove all class in that div
    $(this).removeClass('tct-button').addClass('tct-button2'); //Add the class to current element
});
(您可能希望为父级使用更好的选择器,可能是类或实际html使用的内容)

例如:


如果您想在按钮(tct-button2)上添加一个附加类,同时在每个按钮上保留tct按钮,您可以按照Satpal的建议执行

但是,从它的声音来看,我可能是错的,如果您想更改类,使每个按钮一次只有一个类(tct按钮或tct-button2),您可以执行与Satpal建议的类似的操作并使用:

$('button').on('click', function(){
    $('.tct-button2').addClass('tct-button').removeClass('tct-button2'); //Add original class and Remove tct-button2 class
    $(this).removeClass('tct-button').addClass('tct-button2'); //Add the class to current element
});
这是一个例子

要仅影响单击按钮所属的div中的按钮,请使用类似的方法:

$('button').on('click', function(){
    $(this).parent('div').find('.tct-button2').addClass('tct-button').removeClass('tct-button2'); //Remove all class in that div
    $(this).removeClass('tct-button').addClass('tct-button2'); //Add the class to current element
});
(您可能希望为父级使用更好的选择器,可能是类或实际html使用的内容)

例如:


如果您想在按钮(tct-button2)上添加一个附加类,同时在每个按钮上保留tct按钮,您可以按照Satpal的建议执行

但是,从它的声音来看,我可能是错的,如果您想更改类,使每个按钮一次只有一个类(tct按钮或tct-button2),您可以执行与Satpal建议的类似的操作并使用:

$('button').on('click', function(){
    $('.tct-button2').addClass('tct-button').removeClass('tct-button2'); //Add original class and Remove tct-button2 class
    $(this).removeClass('tct-button').addClass('tct-button2'); //Add the class to current element
});
这是一个例子

要仅影响单击按钮所属的div中的按钮,请使用类似的方法:

$('button').on('click', function(){
    $(this).parent('div').find('.tct-button2').addClass('tct-button').removeClass('tct-button2'); //Remove all class in that div
    $(this).removeClass('tct-button').addClass('tct-button2'); //Add the class to current element
});
(您可能希望为父级使用更好的选择器,可能是类或实际html使用的内容)

例如:


如果您想在按钮(tct-button2)上添加一个附加类,同时在每个按钮上保留tct按钮,您可以按照Satpal的建议执行

但是,从它的声音来看,我可能是错的,如果您想更改类,使每个按钮一次只有一个类(tct按钮或tct-button2),您可以执行与Satpal建议的类似的操作并使用:

$('button').on('click', function(){
    $('.tct-button2').addClass('tct-button').removeClass('tct-button2'); //Add original class and Remove tct-button2 class
    $(this).removeClass('tct-button').addClass('tct-button2'); //Add the class to current element
});
这是一个例子

要仅影响单击按钮所属的div中的按钮,请使用类似的方法:

$('button').on('click', function(){
    $(this).parent('div').find('.tct-button2').addClass('tct-button').removeClass('tct-button2'); //Remove all class in that div
    $(this).removeClass('tct-button').addClass('tct-button2'); //Add the class to current element
});
(您可能希望为父级使用更好的选择器,可能是类或实际html使用的内容)

例如:



但这也会使其余部分保持不变。当我点击一个按钮时,我需要另一个返回到tct按钮(默认类),但这会使其余的也保持更改。当我点击一个按钮时,我需要另一个返回到tct按钮(默认类),但这会使其余的也保持更改。当我点击一个按钮时,我需要另一个返回到tct按钮(默认类),但这会使其余的也保持更改。当我点击一个按钮时,我需要另一个返回tct按钮(默认类)这不改变其他分区中的所有其他tct按钮吗?我只需要改变按钮所属的分区。这不改变其他分区中的所有其他tct按钮吗?我只需要改变按钮所属的分区。这不改变其他分区中的所有其他tct按钮吗?我只需要改变按钮所属的分区。不是吗更改其他分区中的所有其他tct按钮2?我只需要更改按钮所属的分区。谢谢,但问题是我有多个分区,我需要更改按钮所属的分区。你们说的代码正在运行,但更改了所有div中的所有按钮。例如,在您的演示中,如果我单击Div2中的一个按钮,Div1中的那个按钮也在更改。啊,好的。这个问题一开始并不清楚,对不起。更新了我的答案以进一步满足您的要求。谢谢您的代码,但为什么它只在其中一个div中工作,而不在其他div中工作?(哦,谢谢我修好了。非常感谢)我不明白你在问什么,这是你想要的吗?我的代码,点击一个按钮查找该按钮的父级(在本例中,
div
然后查找该父级中具有
tct-button2
类的所有按钮,然后将原始类添加到这些按钮中,并从中删除
tct-button2
类(实际上重置其中的所有按钮)。然后删除单击按钮的原始类,并添加新的
tct-button2