[英]How to get Javascript's “getElementsByClassName()” to work?
When I do this, everything works just fine: 当我这样做时,一切正常:
function openTab(tabName)
{
document.getElementById("divUsers").className = "invisible";
document.getElementById("divGroups").className = "invisible";
document.getElementById("divZFSPools").className = "invisible";
document.getElementById("divShares").className = "invisible";
document.getElementById(tabName).className = "visible";
}
But when I do this , nothing happens: 但是,当我这样做,没有任何反应:
function openTab(tabName)
{
var targetTab, activeTab;
// Get the div:
targetTab = document.getElementById(tabName);
// If it is the active tab, return:
if(targetTab.style.display.className == "visible");
return;
// No, it is not the active tab:
document.getElementsByClassName("visible")[0].className = "invisible";
// Make the target tab visible:
document.getElementById(tabName).className = "visible";
}
FYI: "visible" and "invisible" are two CSS class names. 仅供参考:“可见”和“不可见”是两个CSS类名称。
Does anyone have idea why? 有人知道为什么吗? How can I achieve the desktop tab control behaviour using HTML and Javascript?
如何使用HTML和Javascript实现桌面选项卡控件行为?
If I don't misunderstood you question just remove the ;
如果我没误会您的问题,只需删除
;
after your if
condition because a simple typo ( ;
) can make huge difference to your code. 在
if
条件之后,因为简单的错字( ;
)会对代码产生巨大的影响。
Assume, 假设,
if (0 === 1); { alert("Hello World") }
// equivalent to:
if (0 === 1) /*do nothing*/ ;
alert ("Hello World");
This code will alert "Hello World", but not because 0 equals 1, but because of the semicolon.
该代码将警告“ Hello World”,但不是因为0等于1,而是因为分号。 It makes JavaScript think that you have an empty statement there, and everything to the right of it is treated as no longer belonging to the if conditional and thus independent of it.
它使JavaScript认为您在那里有一个空语句,它右边的所有内容都被视为不再属于if条件,因此独立于if。
Source : https://www.codecademy.com/en/forum_questions/507f6dd09266b70200000d7e 来源 : https : //www.codecademy.com/en/forum_questions/507f6dd09266b70200000d7e
So on your code it will be like this, 因此在您的代码上将是这样,
//If it is the active tab, return:
if(targetTab.style.display.className == "visible");
return; //^^ remove this semicolon
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.