使用按钮切换表格

所以目前我已经制作了 2 个表格,其中包含信息。但我想这样做,当用户打开 html 文档时,它只显示 1 个表,您可以在不同的表之间切换。当我单击表 1 的按钮时,它会隐藏表 2,当我单击表 2 的按钮时,它会隐藏表 1。

function myFunction() {

  var x = document.getElementById("Tables");

  if (x.style.display === "none") {

    x.style.display = "block";

  } else {

    x.style.display = "none";

  }

}


function myFunction1() {

  var x = document.getElementById("Tables1");

  if (x.style.display === "none") {

    x.style.display = "block";

  } else {

    x.style.display = "none";

  }

}

#Tables {

  width: 100%;

  padding: 50px 0;

  text-align: center;

  background-color: blue;

  margin-top: 20px;

}


#Tables1 {

  width: 100%;

  padding: 50px 0;

  text-align: center;

  background-color: Purple;

  margin-top: 20px;

}


table {

  font-family: arial, sans-serif;

  border-collapse: collapse;

  width: 100%;

}


td,

th {

  border: 1px solid #dddddd;

  text-align: left;

  padding: 8px;

}


tr:nth-child(even) {

  background-color: #dddddd;

}

<button onclick="myFunction()">Click!</button> <button onclick="myFunction1()">Click!</button>


<div id="Tables">

  <table>

    <tr>

      <th>Cost</th>

      <th>Name</th>

      <th>Country</th>

    </tr>

    <tr>

      <td>Price</td>

      <td>Names</td>

      <td>Place</td>

    </tr>

  </table>

</div>


<div id="Tables1">

  <table>

    <tr>

      <th>Cost</th>

      <th>Name</th>

      <th>Country</th>

    </tr>

    <tr>

      <td>Price</td>

      <td>Names</td>

      <td>Place</td>

    </tr>

  </table>

</div>


三国纷争
浏览 196回答 3
3回答

SMILET

使用单个选择。并给你想要隐藏的表隐藏的属性并使用下面给出的javascript&nbsp;function myFunction() {&nbsp; &nbsp; &nbsp; var tables = document.getElementsByClassName("toggletable");&nbsp; &nbsp; &nbsp; var targetval = document.getElementById("select").value;&nbsp; &nbsp; &nbsp; for(var i = 0; i < tables.length; i++){&nbsp; &nbsp; &nbsp; &nbsp; if(targetval == i + 1){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tables[i].removeAttribute("hidden");&nbsp; &nbsp; &nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tables[i].setAttribute("hidden", true);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }<!DOCTYPE html><html><head><meta name="viewport" content="width=device-width, initial-scale=1"><style>#Tables {&nbsp; width: 100%;&nbsp; padding: 50px 0;&nbsp; text-align: center;&nbsp; background-color: blue;&nbsp; margin-top: 20px;}#Tables1 {&nbsp; width: 100%;&nbsp; padding: 50px 0;&nbsp; text-align: center;&nbsp; background-color: Purple;&nbsp; margin-top: 20px;}table {&nbsp; font-family: arial, sans-serif;&nbsp; border-collapse: collapse;&nbsp; width: 100%;}td, th {&nbsp; border: 1px solid #dddddd;&nbsp; text-align: left;&nbsp; padding: 8px;}tr:nth-child(even) {&nbsp; background-color: #dddddd;}</style></head><body><select id="select" onchange="myFunction()">&nbsp; &nbsp; <option value="1" >Table 1</option>&nbsp; &nbsp; <option value="2">Table 2</option>&nbsp; &nbsp; <option value="3">Table 3</option>&nbsp; &nbsp; <option value="4">Table 4</option>&nbsp; &nbsp; <option value="5">Table 5</option></select><div id="Tables" class="toggletable"><table>&nbsp; <tr>&nbsp; &nbsp; <th>Cost</th>&nbsp; &nbsp; <th>Name</th>&nbsp; &nbsp; <th>Country</th>&nbsp; </tr>&nbsp; <tr>&nbsp; &nbsp; <td>Price1</td>&nbsp; &nbsp; <td>Names1</td>&nbsp; &nbsp; <td>Place1</td>&nbsp; </tr></table></div><div id="Tables1" class="toggletable" hidden><table>&nbsp; <tr>&nbsp; &nbsp; <th>Cost</th>&nbsp; &nbsp; <th>Name</th>&nbsp; &nbsp; <th>Country</th>&nbsp; </tr>&nbsp; <tr>&nbsp; &nbsp; <td>Price2</td>&nbsp; &nbsp; <td>Names2</td>&nbsp; &nbsp; <td>Place2</td>&nbsp; </tr></table></div><div class="toggletable" hidden><table>&nbsp; <tr>&nbsp; &nbsp; <th>Cost</th>&nbsp; &nbsp; <th>Name</th>&nbsp; &nbsp; <th>Country</th>&nbsp; </tr>&nbsp; <tr>&nbsp; &nbsp; <td>Price3</td>&nbsp; &nbsp; <td>Names3</td>&nbsp; &nbsp; <td>Place3</td>&nbsp; </tr></table></div><div class="toggletable" hidden><table>&nbsp; <tr>&nbsp; &nbsp; <th>Cost</th>&nbsp; &nbsp; <th>Name</th>&nbsp; &nbsp; <th>Country</th>&nbsp; </tr>&nbsp; <tr>&nbsp; &nbsp; <td>Price4</td>&nbsp; &nbsp; <td>Names4</td>&nbsp; &nbsp; <td>Place4</td>&nbsp; </tr></table></div><div class="toggletable" hidden><table>&nbsp; <tr>&nbsp; &nbsp; <th>Cost</th>&nbsp; &nbsp; <th>Name</th>&nbsp; &nbsp; <th>Country</th>&nbsp; </tr>&nbsp; <tr>&nbsp; &nbsp; <td>Price5</td>&nbsp; &nbsp; <td>Names5</td>&nbsp; &nbsp; <td>Place5</td>&nbsp; </tr></table></div><script>function myFunction() {&nbsp; var tables = document.getElementsByClassName("toggletable");&nbsp; var targetval = document.getElementById("select").value;&nbsp; for(var i = 0; i < tables.length; i++){&nbsp; &nbsp; if(targetval == i + 1){&nbsp; &nbsp; &nbsp; &nbsp; tables[i].removeAttribute("hidden");&nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; tables[i].setAttribute("hidden", true);&nbsp; &nbsp; }&nbsp; }}</script></body></html>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript