简体   繁体   English

如何使Javascript的“ getElementsByClassName()”正常工作?

[英]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.

相关问题 如何通过getElementsByClassName()通过javascript获取和使用html中的表? - How to get and use table in html by javascript by getElementsByClassName()? Javascript:getElementsByClassName:如何只获取父母 - Javascript: getElementsByClassName: how to get only parents 如何从 document.getElementsByClassName('myTxtBox'); 获取 JavaScript 中的值 - how to get value in javascript from document.getElementsByClassName('myTxtBox'); 如何检查 getElementsByClassName 的位置? - How to check getElementsByClassName's position? 如何在 getElementsByClassName 中获取当前元素 - How to get current element in getElementsByClassName javascript 的 getElementsByClassName 长度在 angular 中不起作用 - javascript's getElementsByClassName length not working in angular Javascript的getElementsByClassName()无法找到正确的类 - Javascript's getElementsByClassName() is unable to find proper classes Javascript OOP使用getElementsByClassName方法获取子级 - Javascript OOP get Children with getElementsByClassName method Javascript - 无法让'getElementsByClassName'正常工作 - Javascript - Can't get 'getElementsByClassName' working 如何在 JavaScript 中使用 .getElementsByClassName 更改背景颜色? - How to change background color using .getElementsByClassName in JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM