如何将参数传递给 getElementById 方法?

How to pass an argument to a getElementById method?

正在研究一种显示信息的方式,8 个面板(单击每个面板时它会展开下面的内容)- 但是我正在尝试添加样式,以便在单击一个面板时(活动)- 背景颜色更新为绿色并在单击新元素时被删除。我似乎无法将 tabName 参数传递给 getElementById,因此目前该参数被硬编码为 banking-tab(单击时会更新为绿色)。有没有人对最佳解决方案和探索删除功能的潜在方法有任何建议?

function onExpandFuncs(tabName) {
  openTab(tabName);
  styleTab(tabName);
}

function openTab(tabName) {
  var i, x;
  x = document.getElementsByClassName('containerTab');
  for (i = 0; i < x.length; i++) {
    x[i].style.display = 'none';
  }
  document.getElementById(tabName).style.display = 'block';
}

function styleTab(tabName) {
  document.getElementById('banking-tab').classList.add('active-column');
}
* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: Arial, Helvetica, sans-serif;
}

/* The grid: Four equal columns that floats next to each other */
.column {
  background-color: black;
  float: left;
  width: 25%;
  height: 226px;
  padding: 50px;
  text-align: center;
  font-size: 16px;
  cursor: pointer;
  color: white;
}
.active-column {
  background-color: green;
  float: left;
  width: 25%;
  height: 226px;
  padding: 50px;
  text-align: center;
  font-size: 16px;
  cursor: pointer;
  color: white;
}

.containerTab {
  padding: 20px;
  color: white;
}

/* Clear floats after the columns */
.row:after {
  content: '';
  display: table;
  clear: both;
}

/* Closable button inside the container tab */
.closebtn {
  float: right;
  color: white;
  font-size: 35px;
  cursor: pointer;
}

.arrow-down {
  width: 25px;
  height: 25px;
}
<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <link rel="stylesheet" href="styles.css" />
    <script src="index.js"></script>
  </head>
  <body>
    <div style="text-align: center">
      <h2>Expanding Grid</h2>
      <p>Click on the boxes below:</p>
    </div>

    <!-- Four columns -->
    <div class="row">
      <div id="banking-tab" class="column" onclick="onExpandFuncs('b1');">
        <img src="./Icons/Banking.svg" />
        <p>Banking</p>
        <img class="arrow-down" src="./Icons/arrow-down.png" />
      </div>
      <div id="regtech-tab" class="column" onclick="onExpandFuncs('b2');">
        <img src="./Icons/Regtech.svg" />
        <p>RegTech</p>
        <img class="arrow-down" src="./Icons/arrow-down.png" />
      </div>
      <div id="insurtech-tab" class="column" onclick="onExpandFuncs('b3');">
        <img src="./Icons/InsurTech.svg" />
        <p>InsurTech</p>
        <img class="arrow-down" src="./Icons/arrow-down.png" />
      </div>
      <div id="lending-tab" class="column" onclick="onExpandFuncs('b4');">
        <img src="./Icons/Lending.svg" />
        <p>Lending</p>
        <img class="arrow-down" src="./Icons/arrow-down.png" />
      </div>
    </div>

    <!-- Full-width columns: (hidden by default) -->
    <div id="b1" class="containerTab" style="display: none; background: black">
      <span onclick="this.parentElement.style.display='none'" class="closebtn"
        >&times;</span
      >
      <h3>Banking</h3>
      <p>
        Lorem ipsum dolor sit amet, te quo doctus abhorreant, et pri deleniti
        intellegat, te sanctus inermis ullamcorper nam. Ius error diceret
        deseruisse ad
      </p>
    </div>

    <div
      id="b2"
      class="containerTab"
      style="display: none; background: lightgrey"
    >
      <span onclick="this.parentElement.style.display='none'" class="closebtn"
        >&times;</span
      >
      <h2>RegTech</h2>
      <p>
        Lorem ipsum dolor sit amet, te quo doctus abhorreant, et pri deleniti
        intellegat, te sanctus inermis ullamcorper nam. Ius error diceret
        deseruisse ad
      </p>
    </div>

    <div
      id="b3"
      class="containerTab"
      style="display: none; background: lightgrey"
    >
      <span onclick="this.parentElement.style.display='none'" class="closebtn"
        >&times;</span
      >
      <h2>InsurTech</h2>
      <p>
        Lorem ipsum dolor sit amet, te quo doctus abhorreant, et pri deleniti
        intellegat, te sanctus inermis ullamcorper nam. Ius error diceret
        deseruisse ad
      </p>
    </div>

    <div
      id="b4"
      class="containerTab"
      style="display: none; background: lightgrey"
    >
      <span onclick="this.parentElement.style.display='none'" class="closebtn"
        >&times;</span
      >
      <h2>Lending</h2>
      <p>
        Lorem ipsum dolor sit amet, te quo doctus abhorreant, et pri deleniti
        intellegat, te sanctus inermis ullamcorper nam. Ius error diceret
        deseruisse ad
      </p>
    </div>

    <!-- Bottom four columns -->
    <div class="row">
      <div class="column" onclick="openTab('b5');">
        <img src="./Icons/Accounting.svg" />
        <p>Accounting</p>
        <img class="arrow-down" src="./Icons/arrow-down.png" />
      </div>
      <div class="column" onclick="openTab('b6');">
        <img src="./Icons/Payments.svg" />
        <p>Payments</p>
        <img class="arrow-down" src="./Icons/arrow-down.png" />
      </div>
      <div class="column" onclick="openTab('b7');">
        <img src="./Icons/Quote.svg" />
        <p>Quote Aggregators</p>
        <img class="arrow-down" src="./Icons/arrow-down.png" />
      </div>
      <div class="column" onclick="openTab('b8');">
        <img src="./Icons/WealthTech.svg" />
        <p>WealthTech</p>
        <img class="arrow-down" src="./Icons/arrow-down.png" />
      </div>
    </div>

    <div
      id="b5"
      class="containerTab"
      style="display: none; background: lightgrey"
    >
      <span onclick="this.parentElement.style.display='none'" class="closebtn"
        >&times;</span
      >
      <h2>Accounting</h2>
      <p>
        Lorem ipsum dolor sit amet, te quo doctus abhorreant, et pri deleniti
        intellegat, te sanctus inermis ullamcorper nam. Ius error diceret
        deseruisse ad
      </p>
    </div>

    <div
      id="b6"
      class="containerTab"
      style="display: none; background: lightgrey"
    >
      <span onclick="this.parentElement.style.display='none'" class="closebtn"
        >&times;</span
      >
      <h2>Payments</h2>
      <p>
        Lorem ipsum dolor sit amet, te quo doctus abhorreant, et pri deleniti
        intellegat, te sanctus inermis ullamcorper nam. Ius error diceret
        deseruisse ad
      </p>
    </div>

    <div
      id="b7"
      class="containerTab"
      style="display: none; background: lightgrey"
    >
      <span onclick="this.parentElement.style.display='none'" class="closebtn"
        >&times;</span
      >
      <h2>Quote</h2>
      <p>
        Lorem ipsum dolor sit amet, te quo doctus abhorreant, et pri deleniti
        intellegat, te sanctus inermis ullamcorper nam. Ius error diceret
        deseruisse ad
      </p>
    </div>

    <div
      id="b8"
      class="containerTab"
      style="display: none; background: lightgrey"
    >
      <span onclick="this.parentElement.style.display='none'" class="closebtn"
        >&times;</span
      >
      <h2>WealthTech</h2>
      <p>
        Lorem ipsum dolor sit amet, te quo doctus abhorreant, et pri deleniti
        intellegat, te sanctus inermis ullamcorper nam. Ius error diceret
        deseruisse ad
      </p>
    </div>
  </body>
</html>

非常感谢任何帮助!

您需要做的就是遍历所有选项卡元素并在每个选项卡上设置一个 click 处理程序,它将设置 this 的背景颜色,这将代表该元素处理事件。无需知道或使用 id(应尽可能避免使用它们,因为它们的使用会产生脆弱的代码)。

还有don't use .getElementsByClassName().

我删除了与此问题无关的所有 onclick 属性,以避免此处代码中未包含回调的错误。

// Get all the tabs into a collection
// (don't use .getElementsByClassName()!)
let tabs = document.querySelectorAll(".column");

// Loop over all the tabs
tabs.forEach(function(tab){
  // Set up a click event handler on each of the tabs
  tab.addEventListener("click", function(event){
    // Loop over all the tabs and remove the active class
    tabs.forEach(function(tab){
      tab.classList.remove("active-column");
    });
  
    // Set the background of the clicked tab
    this.classList.add("active-column");
  });

});
* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: Arial, Helvetica, sans-serif;
}

/* The grid: Four equal columns that floats next to each other */
.column {
  background-color: black;
  float: left;
  width: 25%;
  height: 226px;
  padding: 50px;
  text-align: center;
  font-size: 16px;
  cursor: pointer;
  color: white;
}
.active-column {
  background-color: green;
  float: left;
  width: 25%;
  height: 226px;
  padding: 50px;
  text-align: center;
  font-size: 16px;
  cursor: pointer;
  color: white;
}

.containerTab {
  padding: 20px;
  color: white;
}

/* Clear floats after the columns */
.row:after {
  content: '';
  display: table;
  clear: both;
}

/* Closable button inside the container tab */
.closebtn {
  float: right;
  color: white;
  font-size: 35px;
  cursor: pointer;
}

.arrow-down {
  width: 25px;
  height: 25px;
}
<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <link rel="stylesheet" href="styles.css" />
    <script src="index.js"></script>
  </head>
  <body>
    <div style="text-align: center">
      <h2>Expanding Grid</h2>
      <p>Click on the boxes below:</p>
    </div>

    <!-- Four columns -->
    <div class="row">
      <div id="banking-tab" class="column">
        <img src="./Icons/Banking.svg" />
        <p>Banking</p>
        <img class="arrow-down" src="./Icons/arrow-down.png" />
      </div>
      <div id="regtech-tab" class="column">
        <img src="./Icons/Regtech.svg" />
        <p>RegTech</p>
        <img class="arrow-down" src="./Icons/arrow-down.png" />
      </div>
      <div class="column">
        <img src="./Icons/InsurTech.svg" />
        <p>InsurTech</p>
        <img class="arrow-down" src="./Icons/arrow-down.png" />
      </div>
      <div class="column">
        <img src="./Icons/Lending.svg" />
        <p>Lending</p>
        <img class="arrow-down" src="./Icons/arrow-down.png" />
      </div>
    </div>

    <!-- Full-width columns: (hidden by default) -->
    <div id="b1" class="containerTab" style="display: none; background: black">
      <span onclick="this.parentElement.style.display='none'" class="closebtn"
        >&times;</span
      >
      <h3>Banking</h3>
      <p>
        Lorem ipsum dolor sit amet, te quo doctus abhorreant, et pri deleniti
        intellegat, te sanctus inermis ullamcorper nam. Ius error diceret
        deseruisse ad
      </p>
    </div>

    <div
      id="b2"
      class="containerTab"
      style="display: none; background: lightgrey"
    >
      <span class="closebtn"
        >&times;</span
      >
      <h2>RegTech</h2>
      <p>
        Lorem ipsum dolor sit amet, te quo doctus abhorreant, et pri deleniti
        intellegat, te sanctus inermis ullamcorper nam. Ius error diceret
        deseruisse ad
      </p>
    </div>

    <div
      id="b3"
      class="containerTab"
      style="display: none; background: lightgrey"
    >
      <span class="closebtn"
        >&times;</span
      >
      <h2>InsurTech</h2>
      <p>
        Lorem ipsum dolor sit amet, te quo doctus abhorreant, et pri deleniti
        intellegat, te sanctus inermis ullamcorper nam. Ius error diceret
        deseruisse ad
      </p>
    </div>

    <div
      id="b4"
      class="containerTab"
      style="display: none; background: lightgrey"
    >
      <span class="closebtn"
        >&times;</span
      >
      <h2>Lending</h2>
      <p>
        Lorem ipsum dolor sit amet, te quo doctus abhorreant, et pri deleniti
        intellegat, te sanctus inermis ullamcorper nam. Ius error diceret
        deseruisse ad
      </p>
    </div>

    <!-- Bottom four columns -->
    <div class="row">
      <div class="column" >
        <img src="./Icons/Accounting.svg" />
        <p>Accounting</p>
        <img class="arrow-down" src="./Icons/arrow-down.png" />
      </div>
      <div class="column" >
        <img src="./Icons/Payments.svg" />
        <p>Payments</p>
        <img class="arrow-down" src="./Icons/arrow-down.png" />
      </div>
      <div class="column" >
        <img src="./Icons/Quote.svg" />
        <p>Quote Aggregators</p>
        <img class="arrow-down" src="./Icons/arrow-down.png" />
      </div>
      <div class="column" >
        <img src="./Icons/WealthTech.svg" />
        <p>WealthTech</p>
        <img class="arrow-down" src="./Icons/arrow-down.png" />
      </div>
    </div>

    <div
      id="b5"
      class="containerTab"
      style="display: none; background: lightgrey"
    >
      <span onclick="this.parentElement.style.display='none'" class="closebtn"
        >&times;</span
      >
      <h2>Accounting</h2>
      <p>
        Lorem ipsum dolor sit amet, te quo doctus abhorreant, et pri deleniti
        intellegat, te sanctus inermis ullamcorper nam. Ius error diceret
        deseruisse ad
      </p>
    </div>

    <div
      id="b6"
      class="containerTab"
      style="display: none; background: lightgrey"
    >
      <span onclick="this.parentElement.style.display='none'" class="closebtn"
        >&times;</span
      >
      <h2>Payments</h2>
      <p>
        Lorem ipsum dolor sit amet, te quo doctus abhorreant, et pri deleniti
        intellegat, te sanctus inermis ullamcorper nam. Ius error diceret
        deseruisse ad
      </p>
    </div>

    <div
      id="b7"
      class="containerTab"
      style="display: none; background: lightgrey"
    >
      <span onclick="this.parentElement.style.display='none'" class="closebtn"
        >&times;</span
      >
      <h2>Quote</h2>
      <p>
        Lorem ipsum dolor sit amet, te quo doctus abhorreant, et pri deleniti
        intellegat, te sanctus inermis ullamcorper nam. Ius error diceret
        deseruisse ad
      </p>
    </div>

    <div
      id="b8"
      class="containerTab"
      style="display: none; background: lightgrey"
    >
      <span onclick="this.parentElement.style.display='none'" class="closebtn"
        >&times;</span
      >
      <h2>WealthTech</h2>
      <p>
        Lorem ipsum dolor sit amet, te quo doctus abhorreant, et pri deleniti
        intellegat, te sanctus inermis ullamcorper nam. Ius error diceret
        deseruisse ad
      </p>
    </div>
  </body>
</html>

您正在尝试使用两个不同的元素。 因此,您需要在点击功能中传递两个不同的 ID。如下所示

function onExpandFuncs(tabName, clickedTab) {
  openTab(tabName);
  genericStyleTab(clickedTab);
}

function genericStyleTab(clickedTab) {
  document.getElementById(clickedTab).classList.add('active-column');
}
      <div id="banking-tab" class="column" onclick="onExpandFuncs('b1', 'banking-tab');">