這段代碼基本上使得我的頁面上有7個標籤,onclick,改變另一個div標籤的內容。代碼有效,但時間太長。我想也許有些事情要做,否則如果語句,但我是新來的JavaScript,我不認爲我在那裏呢。如何簡化我的javascript代碼,看起來太長而且凌亂?
<script>
function showBaseChakra(text) {
if(text.id == "baseChakra") {
document.getElementById("baseChakraInfo").style.display = "block";
document.getElementById("defaultText").style.display = "none";
document.getElementById("sacralChakraInfo").style.display = "none";
document.getElementById("solarPlexusChakraInfo").style.display = "none";
document.getElementById("heartChakraInfo").style.display = "none";
document.getElementById("throatChakraInfo").style.display = "none";
document.getElementById("browChakraInfo").style.display = "none";
document.getElementById("crownChakraInfo").style.display = "none";
}
}
function showSacralChakra(text) {
if(text.id == "sacralChakra") {
document.getElementById("sacralChakraInfo").style.display = "block";
document.getElementById("defaultText").style.display = "none";
document.getElementById("baseChakraInfo").style.display = "none";
document.getElementById("solarPlexusChakraInfo").style.display = "none";
document.getElementById("heartChakraInfo").style.display = "none";
document.getElementById("throatChakraInfo").style.display = "none";
document.getElementById("browChakraInfo").style.display = "none";
document.getElementById("crownChakraInfo").style.display = "none";
}
}
function showSolarPlexusChakra(text) {
if(text.id == "solarPlexusChakra") {
document.getElementById("solarPlexusChakraInfo").style.display = "block";
document.getElementById("defaultText").style.display = "none";
document.getElementById("baseChakraInfo").style.display = "none";
document.getElementById("sacralChakraInfo").style.display = "none";
document.getElementById("heartChakraInfo").style.display = "none";
document.getElementById("throatChakraInfo").style.display = "none";
document.getElementById("browChakraInfo").style.display = "none";
document.getElementById("crownChakraInfo").style.display = "none";
}
}
function showHeartChakra(text) {
if(text.id == "heartChakra") {
document.getElementById("heartChakraInfo").style.display = "block";
document.getElementById("defaultText").style.display = "none";
document.getElementById("baseChakraInfo").style.display = "none";
document.getElementById("sacralChakraInfo").style.display = "none";
document.getElementById("solarPlexusChakraInfo").style.display = "none";
document.getElementById("throatChakraInfo").style.display = "none";
document.getElementById("browChakraInfo").style.display = "none";
document.getElementById("crownChakraInfo").style.display = "none";
}
}
function showThroatChakra(text) {
if(text.id == "throatChakra") {
document.getElementById("throatChakraInfo").style.display = "block";
document.getElementById("defaultText").style.display = "none";
document.getElementById("baseChakraInfo").style.display = "none";
document.getElementById("sacralChakraInfo").style.display = "none";
document.getElementById("solarPlexusChakraInfo").style.display = "none";
document.getElementById("heartChakraInfo").style.display = "none";
document.getElementById("browChakraInfo").style.display = "none";
document.getElementById("crownChakraInfo").style.display = "none";
}
}
function showBrowChakra(text) {
if(text.id == "browChakra") {
document.getElementById("browChakraInfo").style.display = "block";
document.getElementById("defaultText").style.display = "none";
document.getElementById("baseChakraInfo").style.display = "none";
document.getElementById("sacralChakraInfo").style.display = "none";
document.getElementById("solarPlexusChakraInfo").style.display = "none";
document.getElementById("heartChakraInfo").style.display = "none";
document.getElementById("throatChakraInfo").style.display = "none";
document.getElementById("crownChakraInfo").style.display = "none";
}
}
function showCrownChakra(text) {
if(text.id == "crownChakra") {
document.getElementById("crownChakraInfo").style.display = "block";
document.getElementById("defaultText").style.display = "none";
document.getElementById("baseChakraInfo").style.display = "none";
document.getElementById("sacralChakraInfo").style.display = "none";
document.getElementById("solarPlexusChakraInfo").style.display = "none";
document.getElementById("heartChakraInfo").style.display = "none";
document.getElementById("throatChakraInfo").style.display = "none";
document.getElementById("browChakraInfo").style.display = "none";
}
}
</script>
給元素賦予相同的類.U唱課你可以選擇所有元素並立即隱藏。然後使所需的元素可見。 – Avi