gpt4 book ai didi

javascript - 具有相似功能时简化 Javascript

转载 作者:行者123 更新时间:2023-11-30 05:57:19 25 4
gpt4 key购买 nike

<分区>

我终于拼凑了一些适用于我的问题的代码 here .但是,它看起来很长,因为我已经为刷新具有特定哈希的页面以及通过单击选项卡访问同一页面的时间创建了单独的函数。

$(document).ready(function () {
$(function () {
var loc = window.location.href; // For when Hazel is refreshed
if (/Hazel/.test(loc)) {
$("#tab1,#tab2,#tab3,#tab4").removeClass("r p c").addClass("h");
$("#tab2").removeClass("tail");
$("#tab3, #tab4").addClass("tail");
}
});
$(function () {
var loc = window.location.href; // For when Red is refreshed
if (/Red/.test(loc)) {
$("#tab1,#tab2,#tab3,#tab4").removeClass("h p c").addClass("r");
$("#tab3, #tab2").removeClass("tail");
$("#tab4").addClass("tail");
}
});
$(function () {
var loc = window.location.href; // For when Pink is refreshed
if (/Pink/.test(loc)) {
$("#tab1,#tab2,#tab3,#tab4").removeClass("h r c").addClass("p");
$("#tab3, #tab4").removeClass("tail");
$("#tab2").addClass("tail");
}
});
});
$(function () {
var loc = window.location.href; // For when Cyan is refreshed
if (/Cyan/.test(loc)) {
$("#tab1,#tab2,#tab3,#tab4").removeClass("h r p").addClass("c");
$("#tab4").removeClass("tail");
$("#tab3, #tab2").addClass("tail");
}
});
$("#tab1").click(function () {
$(window).bind("hashchange", function () {
var loc = window.location.href; // For when Hazel tab is clicked
if (/Hazel/.test(loc)) {
$("#tab1,#tab2,#tab3,#tab4").removeClass("r p c").addClass("h");
$("#tab2").removeClass("tail");
$("#tab3, #tab4").addClass("tail");
}
});
});
$("#tab2").click(function () {
$(window).bind("hashchange", function () {
var loc = window.location.href; // For when Red tab is clicked
if (/Red/.test(loc)) {
$("#tab1,#tab2,#tab3,#tab4").removeClass("h p c").addClass("r");
$("#tab3, #tab2").removeClass("tail");
$("#tab4").addClass("tail");
}
});
});
$("#tab3").click(function () {
$(window).bind("hashchange", function () {
var loc = window.location.href; // For when Pink tab is clicked
if (/Pink/.test(loc)) {
$("#tab1,#tab2,#tab3,#tab4").removeClass("h r c").addClass("p");
$("#tab3, #tab4").removeClass("tail");
$("#tab2").addClass("tail");
}
});
});
$("#tab4").click(function () {
$(window).bind("hashchange", function () {
var loc = window.location.href; // For when Cyan tab is clicked
if (/Cyan/.test(loc)) {
$("#tab1,#tab2,#tab3,#tab4").removeClass("h r p").addClass("c");
$("#tab4").removeClass("tail");
$("#tab3, #tab2").addClass("tail");
}
});
});
});

可以简化吗?我已经尝试过,但到目前为止,我的尝试只是破坏了代码。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com