Jquery 减少冗余代码

Jquery 减少冗余代码,jquery,html,dom,Jquery,Html,Dom,我有这个非常简单的功能,可以从html元素中查找、切换和删除类。问题是,我发现自己用不同的名称类重复相同的代码。我基本上写了9次同样的方法。我知道你总是一遍又一遍地写同样的东西,有一种更有效的方法。所以我只是想看看如何使我的代码更有效 我把我的代码附在我发现自己重复了很多的地方。我尝试了一些想法,但我只保存了大约2行代码 // SECTION 5 $("body").on("tap", ".state1, .display-state1", function showState1() {

我有这个非常简单的功能,可以从html元素中查找、切换和删除类。问题是,我发现自己用不同的名称类重复相同的代码。我基本上写了9次同样的方法。我知道你总是一遍又一遍地写同样的东西,有一种更有效的方法。所以我只是想看看如何使我的代码更有效

我把我的代码附在我发现自己重复了很多的地方。我尝试了一些想法,但我只保存了大约2行代码

// SECTION 5

$("body").on("tap", ".state1, .display-state1", function showState1() {
          $(".show-overlay").toggleClass("display-state1");
          $(".show-overlay").removeClass("display-state2");
          $(".show-overlay").removeClass("display-state3");
    });

$("body").on("tap", ".state2, .display-state2", function showState2() {
          $(".show-overlay").toggleClass("display-state2");
          $(".show-overlay").removeClass("display-state1");
          $(".show-overlay").removeClass("display-state3");
    });

$("body").on("tap", ".state3, .display-state3", function showState3() {
          $(".show-overlay").toggleClass("display-state3");
          $(".show-overlay").removeClass("display-state1");
          $(".show-overlay").removeClass("display-state2");
    });

// SECTION 8

$("body").on("tap", ".state-1, .display-state-1", function showSection8State1() {
          $(".show-overlay-big").toggleClass("display-state-1");
          $(".state-1-pdfs").children().toggleClass("display-pdfs");
          $(".state-2-pdfs").children().removeClass("display-pdfs");
          $(".state-3-pdfs").children().removeClass("display-pdfs");
          $(".state-4-pdfs").children().removeClass("display-pdfs");
          $(".state-5-pdfs").children().removeClass("display-pdfs");
          $(".state-6-pdfs").children().removeClass("display-pdfs");
          $(".state-7-pdfs").children().removeClass("display-pdfs");
          $(".state-8-pdfs").children().removeClass("display-pdfs");
          $(".state-9-pdfs").children().removeClass("display-pdfs");
          $(".show-overlay-big").removeClass("display-state-2");
          $(".show-overlay-big").removeClass("display-state-3");
          $(".show-overlay-big").removeClass("display-state-4");
          $(".show-overlay-big").removeClass("display-state-5");
          $(".show-overlay-big").removeClass("display-state-6");
          $(".show-overlay-big").removeClass("display-state-7");
          $(".show-overlay-big").removeClass("display-state-8");
          $(".show-overlay-big").removeClass("display-state-9");
    });

$("body").on("tap", ".state-2, .display-state-2", function showSection8State2() {
          $(".show-overlay-big").toggleClass("display-state-2");
          $(".state-2-pdfs").children().toggleClass("display-pdfs");
          $(".state-1-pdfs").children().removeClass("display-pdfs");
          $(".state-3-pdfs").children().removeClass("display-pdfs");
          $(".state-4-pdfs").children().removeClass("display-pdfs");
          $(".state-5-pdfs").children().removeClass("display-pdfs");
          $(".state-6-pdfs").children().removeClass("display-pdfs");
          $(".state-7-pdfs").children().removeClass("display-pdfs");
          $(".state-8-pdfs").children().removeClass("display-pdfs");
          $(".state-9-pdfs").children().removeClass("display-pdfs");
          $(".show-overlay-big").removeClass("display-state-1");
          $(".show-overlay-big").removeClass("display-state-3");
          $(".show-overlay-big").removeClass("display-state-4");
          $(".show-overlay-big").removeClass("display-state-5");
          $(".show-overlay-big").removeClass("display-state-6");
          $(".show-overlay-big").removeClass("display-state-7");
          $(".show-overlay-big").removeClass("display-state-8");
          $(".show-overlay-big").removeClass("display-state-9");
    });

$("body").on("tap", ".state-3, .display-state-3", function showSection8State3() {
          $(".show-overlay-big").toggleClass("display-state-3");
          $(".state-3-pdfs").children().toggleClass("display-pdfs");
          $(".state-2-pdfs").children().removeClass("display-pdfs");
          $(".state-1-pdfs").children().removeClass("display-pdfs");
          $(".state-4-pdfs").children().removeClass("display-pdfs");
          $(".state-5-pdfs").children().removeClass("display-pdfs");
          $(".state-6-pdfs").children().removeClass("display-pdfs");
          $(".state-7-pdfs").children().removeClass("display-pdfs");
          $(".state-8-pdfs").children().removeClass("display-pdfs");
          $(".state-9-pdfs").children().removeClass("display-pdfs");
          $(".show-overlay-big").removeClass("display-state-2");
          $(".show-overlay-big").removeClass("display-state-1");
          $(".show-overlay-big").removeClass("display-state-4");
          $(".show-overlay-big").removeClass("display-state-5");
          $(".show-overlay-big").removeClass("display-state-6");
          $(".show-overlay-big").removeClass("display-state-7");
          $(".show-overlay-big").removeClass("display-state-8");
          $(".show-overlay-big").removeClass("display-state-9");
    });

$("body").on("tap", ".state-4, .display-state-4", function showSection8State4() {
          $(".show-overlay-big").toggleClass("display-state-4");
          $(".state-4-pdfs").children().toggleClass("display-pdfs");
          $(".state-2-pdfs").children().removeClass("display-pdfs");
          $(".state-3-pdfs").children().removeClass("display-pdfs");
          $(".state-1-pdfs").children().removeClass("display-pdfs");
          $(".state-5-pdfs").children().removeClass("display-pdfs");
          $(".state-6-pdfs").children().removeClass("display-pdfs");
          $(".state-7-pdfs").children().removeClass("display-pdfs");
          $(".state-8-pdfs").children().removeClass("display-pdfs");
          $(".state-9-pdfs").children().removeClass("display-pdfs");
          $(".show-overlay-big").removeClass("display-state-2");
          $(".show-overlay-big").removeClass("display-state-3");
          $(".show-overlay-big").removeClass("display-state-1");
          $(".show-overlay-big").removeClass("display-state-5");
          $(".show-overlay-big").removeClass("display-state-6");
          $(".show-overlay-big").removeClass("display-state-7");
          $(".show-overlay-big").removeClass("display-state-8");
          $(".show-overlay-big").removeClass("display-state-9");
    });

$("body").on("tap", ".state-5, .display-state-5", function showSection8State5() {
          $(".show-overlay-big").toggleClass("display-state-5");
          $(".state-5-pdfs").children().toggleClass("display-pdfs");
          $(".state-2-pdfs").children().removeClass("display-pdfs");
          $(".state-3-pdfs").children().removeClass("display-pdfs");
          $(".state-4-pdfs").children().removeClass("display-pdfs");
          $(".state-1-pdfs").children().removeClass("display-pdfs");
          $(".state-6-pdfs").children().removeClass("display-pdfs");
          $(".state-7-pdfs").children().removeClass("display-pdfs");
          $(".state-8-pdfs").children().removeClass("display-pdfs");
          $(".state-9-pdfs").children().removeClass("display-pdfs");
          $(".show-overlay-big").removeClass("display-state-2");
          $(".show-overlay-big").removeClass("display-state-3");
          $(".show-overlay-big").removeClass("display-state-4");
          $(".show-overlay-big").removeClass("display-state-1");
          $(".show-overlay-big").removeClass("display-state-6");
          $(".show-overlay-big").removeClass("display-state-7");
          $(".show-overlay-big").removeClass("display-state-8");
          $(".show-overlay-big").removeClass("display-state-9");
    });

$("body").on("tap", ".state-6, .display-state-6", function showSection8State6() {
          $(".show-overlay-big").toggleClass("display-state-6");
          $(".state-6-pdfs").children().toggleClass("display-pdfs");
          $(".state-2-pdfs").children().removeClass("display-pdfs");
          $(".state-3-pdfs").children().removeClass("display-pdfs");
          $(".state-4-pdfs").children().removeClass("display-pdfs");
          $(".state-5-pdfs").children().removeClass("display-pdfs");
          $(".state-1-pdfs").children().removeClass("display-pdfs");
          $(".state-7-pdfs").children().removeClass("display-pdfs");
          $(".state-8-pdfs").children().removeClass("display-pdfs");
          $(".state-9-pdfs").children().removeClass("display-pdfs");
          $(".show-overlay-big").removeClass("display-state-2");
          $(".show-overlay-big").removeClass("display-state-3");
          $(".show-overlay-big").removeClass("display-state-4");
          $(".show-overlay-big").removeClass("display-state-5");
          $(".show-overlay-big").removeClass("display-state-1");
          $(".show-overlay-big").removeClass("display-state-7");
          $(".show-overlay-big").removeClass("display-state-8");
          $(".show-overlay-big").removeClass("display-state-9");
    });

$("body").on("tap", ".state-7, .display-state-7", function showSection8State7() {
          $(".show-overlay-big").toggleClass("display-state-7");
          $(".state-7-pdfs").children().toggleClass("display-pdfs");
          $(".state-2-pdfs").children().removeClass("display-pdfs");
          $(".state-3-pdfs").children().removeClass("display-pdfs");
          $(".state-4-pdfs").children().removeClass("display-pdfs");
          $(".state-5-pdfs").children().removeClass("display-pdfs");
          $(".state-6-pdfs").children().removeClass("display-pdfs");
          $(".state-1-pdfs").children().removeClass("display-pdfs");
          $(".state-8-pdfs").children().removeClass("display-pdfs");
          $(".state-9-pdfs").children().removeClass("display-pdfs");
          $(".show-overlay-big").removeClass("display-state-2");
          $(".show-overlay-big").removeClass("display-state-3");
          $(".show-overlay-big").removeClass("display-state-4");
          $(".show-overlay-big").removeClass("display-state-5");
          $(".show-overlay-big").removeClass("display-state-6");
          $(".show-overlay-big").removeClass("display-state-1");
          $(".show-overlay-big").removeClass("display-state-8");
          $(".show-overlay-big").removeClass("display-state-9");
    });

$("body").on("tap", ".state-8, .display-state-8", function showSection8State8() {
          $(".show-overlay-big").toggleClass("display-state-8");
          $(".state-8-pdfs").children().toggleClass("display-pdfs");
          $(".state-2-pdfs").children().removeClass("display-pdfs");
          $(".state-3-pdfs").children().removeClass("display-pdfs");
          $(".state-4-pdfs").children().removeClass("display-pdfs");
          $(".state-5-pdfs").children().removeClass("display-pdfs");
          $(".state-6-pdfs").children().removeClass("display-pdfs");
          $(".state-7-pdfs").children().removeClass("display-pdfs");
          $(".state-1-pdfs").children().removeClass("display-pdfs");
          $(".state-9-pdfs").children().removeClass("display-pdfs");
          $(".show-overlay-big").removeClass("display-state-2");
          $(".show-overlay-big").removeClass("display-state-3");
          $(".show-overlay-big").removeClass("display-state-4");
          $(".show-overlay-big").removeClass("display-state-5");
          $(".show-overlay-big").removeClass("display-state-6");
          $(".show-overlay-big").removeClass("display-state-7");
          $(".show-overlay-big").removeClass("display-state-1");
          $(".show-overlay-big").removeClass("display-state-9");
    });

$("body").on("tap", ".state-9, .display-state-9", function showSection8State9() {
          $(".show-overlay-big").toggleClass("display-state-9");
          $(".state-9-pdfs").children().toggleClass("display-pdfs");
          $(".state-2-pdfs").children().removeClass("display-pdfs");
          $(".state-3-pdfs").children().removeClass("display-pdfs");
          $(".state-4-pdfs").children().removeClass("display-pdfs");
          $(".state-5-pdfs").children().removeClass("display-pdfs");
          $(".state-6-pdfs").children().removeClass("display-pdfs");
          $(".state-7-pdfs").children().removeClass("display-pdfs");
          $(".state-8-pdfs").children().removeClass("display-pdfs");
          $(".state-1-pdfs").children().removeClass("display-pdfs");
          $(".show-overlay-big").removeClass("display-state-2");
          $(".show-overlay-big").removeClass("display-state-3");
          $(".show-overlay-big").removeClass("display-state-4");
          $(".show-overlay-big").removeClass("display-state-5");
          $(".show-overlay-big").removeClass("display-state-6");
          $(".show-overlay-big").removeClass("display-state-7");
          $(".show-overlay-big").removeClass("display-state-8");
          $(".show-overlay-big").removeClass("display-state-1");
    });

以下是一些帮助您开始的方法:

$("body").on("tap", ".state1, .display-state1", function showState1() {
    $(".show-overlay").toggleClass("display-state1").removeClass("display-state2 display-state3");
});


但这一切看起来有点可疑。毫无疑问,有一种更好的方法来处理您正在做的事情,但如果没有相关的HTML,就无法判断。以下是一些帮助您入门的方法:

$("body").on("tap", ".state1, .display-state1", function showState1() {
    $(".show-overlay").toggleClass("display-state1").removeClass("display-state2 display-state3");
});


但这一切看起来有点可疑。毫无疑问,有一种更好的方法来处理您正在做的事情,但如果没有相关的HTML,就无法判断。以下是一些帮助您入门的方法:

$("body").on("tap", ".state1, .display-state1", function showState1() {
    $(".show-overlay").toggleClass("display-state1").removeClass("display-state2 display-state3");
});


但这一切看起来有点可疑。毫无疑问,有一种更好的方法来处理您正在做的事情,但如果没有相关的HTML,就无法判断。以下是一些帮助您入门的方法:

$("body").on("tap", ".state1, .display-state1", function showState1() {
    $(".show-overlay").toggleClass("display-state1").removeClass("display-state2 display-state3");
});


但这一切看起来有点可疑。毫无疑问,有一种更好的方法来处理您正在做的事情,但是如果没有相关的HTML,就无法判断。

假设您有格式
state-#-pdfs
的类,其中
在[1,9]范围内。如果您想影响这些类的一个子集,比如在[5,9]范围内,您可以使用简单的
for
循环进行迭代:

var i;
for( i=5; i<=9; i++ )
{
    $("state-"+i+"-pdfs").children().removeClass("display-pdfs");
}
vari;

对于(i=5;i假设您有格式为
state-#-pdfs
的类,其中
#
在[1,9]范围内。如果您想影响这些类的子集,比如在[5,9]范围内,您可以通过一个简单的
for
循环进行迭代:

var i;
for( i=5; i<=9; i++ )
{
    $("state-"+i+"-pdfs").children().removeClass("display-pdfs");
}
vari;

对于(i=5;i假设您有格式为
state-#-pdfs
的类,其中
#
在[1,9]范围内。如果您想影响这些类的子集,比如在[5,9]范围内,您可以通过一个简单的
for
循环进行迭代:

var i;
for( i=5; i<=9; i++ )
{
    $("state-"+i+"-pdfs").children().removeClass("display-pdfs");
}
vari;

对于(i=5;i假设您有格式为
state-#-pdfs
的类,其中
#
在[1,9]范围内。如果您想影响这些类的子集,比如在[5,9]范围内,您可以通过一个简单的
for
循环进行迭代:

var i;
for( i=5; i<=9; i++ )
{
    $("state-"+i+"-pdfs").children().removeClass("display-pdfs");
}
vari;
对于(i=5;i你可以这样做:

//第5节的变量
var classes5=[“display-state1”、“display-state2”、“display-state3”];
函数showState(类名称){
对于(i=0;i
您可以这样做:

//第5节的变量
var classes5=[“display-state1”、“display-state2”、“display-state3”];
函数showState(类名称){
对于(i=0;i