gpt4 book ai didi

javascript - 如何缩短长重复路径和组合语句

转载 作者:太空宇宙 更新时间:2023-11-04 16:26:10 25 4
gpt4 key购买 nike

对于一个网站,我使用了 9 种不同外观的选项卡,这些选项卡用于不同的情况。一切正常,但我使用了很多带有路径和语句的函数,我认为这些函数可能会更短。一个例子:

function tabAon() {
tabAvoor=tabAvoor+1;
tabDrawer=tabDrawer+1;
tabBvoor=0;
tabCvoor=0;
window.document.getElementById("tabA").style.textDecoration="none";
window.document.getElementById("tabB").style.backgroundPosition="-87px -228px";
window.document.getElementById("tabB").style.color="#898f92";
window.document.getElementById("tabC").style.backgroundPosition="-190px -228px";
window.document.getElementById("tabC").style.color="#898f92";
window.document.getElementById("tabA").style.cursor="pointer";
if (tabDrawer==0) {
window.document.getElementById("header").MozTransform="translateY(0px)";
window.document.getElementById("header").msTransform="translateY(0px)";
window.document.getElementById("header").OTransform="translateY(0px)";
window.document.getElementById("header").WebkitTransform="translateY(0px)";
window.document.getElementById("header").transform="translateY(0px)";
window.document.getElementById("tabA").style.color="#898f92";
window.document.getElementById("tabA").style.backgroundPosition="0px 0px";
}
if (tabDrawer==1) {
window.document.getElementById("header").style.MozTransform="translateY(36px)";
window.document.getElementById("header").style.msTransform="translateY(36px)";
window.document.getElementById("header").style.OTransform="translateY(36px)";
window.document.getElementById("header").style.WebkitTransform="translateY(36px)";
window.document.getElementById("header").style.transform="translateY(36px)";
window.document.getElementById("tabAout").style.visibility="visible";
window.document.getElementById("tabBout").style.visibility="hidden";
window.document.getElementById("tabCout").style.visibility="hidden";
window.document.getElementById("tabA").style.color="#000000";
window.document.getElementById("tabA").style.backgroundPosition="0px -114px";
}
if (tabAvoor==2) tabAvoor=1;
if (tabDrawer==2 && tabBvoor==0 && tabCvoor==0) tabDrawer=0;
if (tabDrawer==2 && tabBvoor==1 || tabCvoor==1) tabDrawer=1;
}

最佳答案

使用 JS 框架怎么样?例如: jQuery

例子:

 $('#tabBout').show();

代替 window.document.getElementById("tabAout").style.visibility="visible";

关于javascript - 如何缩短长重复路径和组合语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5492496/

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