单击分页按钮后如何使表格可见?

时间:2018-05-26 03:31:37

标签: javascript html css bootstrap-4

所以我正在尝试创建一个分页样式部分,用户可以在其中更改表信息。我可以说,当我按“2”时,第二个表出现,当我点击“1”或“<”它返回第一个表。我遇到的问题是,当我第一次启动它时,两个表都出现在页面上,当我使用分页时,它只显示一个表。

我尝试将CS​​S代码放在属性display: none上并且它有效但最终当我点击“2”按钮时它不会出现。

这是我的代码:

   <div class="col-sm-8">
    <div class="col-sm-8c col-md-12 bg-light">
      <div class="col-sm-8 col-md-12">
        <div id="tabla1" class="tabcontent">
          <div class="table-responsive my-3">
            <table class="table table table-striped table-hover my-2" style="background-color: #f5f6fa">
              <thead>
                <tr>
                  <th>#</th>
                  <th>Name</th>
                  <th>Faculty</th>
                  <th></th>
                  <th>Progress of<br>donations</th>
                </tr>
              </thead>
              <tbody>
              <tr>
                <td>1</td>
                <td>Mary</td>
                <td>FCQI</td>
                <td>
                  <center>
                    <button type="button" class="btn btn-primary btn-sm">Donar</button>
                  </center>
                </td>
                <th>
                  <div class="progress">
                    <div class="progress-bar progress-bar-striped progress-bar-animated" style="width:60%"></div>
                  </div>
                </th>
              </tr>
              <tr>
                <td>2</td>
                <td>Mary</td>
                <td>FCQI</td>
                <td>
                  <center>
                    <button type="button" class="btn btn-primary btn-sm">Donar</button>
                  </center>
                </td>
                <th>
                  <div class="progress">
                    <div class="progress-bar progress-bar-striped progress-bar-animated" style="width:20%"></div>
                  </div>
                </th>
              </tr>
              <tr>
                <td>3</td>
                <td>Mariana</td>
                <td>FCQI</td>
                <td>
                  <center>
                    <button type="button" class="btn btn-primary btn-sm">Donar</button>
                  </center>
                </td>
                <th>
                  <div class="progress">
                    <div class="progress-bar progress-bar-striped progress-bar-animated" style="width:10%"></div>
                  </div>
                </th>
              </tr>
              <tr>
                <td>4</td>
                <td>Evelyn</td>
                <td>FCQI</td>
                <td>
                  <center>
                    <button type="button" class="btn btn-primary btn-sm">Donar</button>
                  </center>
                </td>
                <th>
                  <div class="progress">
                    <div class="progress-bar progress-bar-striped progress-bar-animated" style="width:80%"></div>
                  </div>
                </th>
              </tr>
              <tr>
                <td>5</td>
                <td>Mary</td>
                <td>FCQI</td>
                <td>
                  <center>
                    <button type="button" class="btn btn-primary btn-sm">Donar</button>
                  </center>
                </td>
                <th>
                  <div class="progress">
                    <div class="progress-bar progress-bar-striped progress-bar-animated" style="width:0%"></div>
                  </div>
                </th>
              </tr>
              <tr>
                <td>6</td>
                <td>Mary</td>
                <td>FCQI</td>
                <td>
                  <center>
                    <button type="button" class="btn btn-primary btn-sm">Donar</button>
                  </center>
                </td>
                <th>
                  <div class="progress">
                    <div class="progress-bar progress-bar-striped progress-bar-animated" style="width:40%"></div>
                  </div>
                </th>
              </tr>
              <tr>
                <td>7</td>
                <td>Sebastian</td>
                <td>FCQI</td>
                <td>
                  <center>
                    <button type="button" class="btn btn-primary btn-sm">Donar</button>
                  </center>
                </td>
                <th>
                  <div class="progress">
                    <div class="progress-bar progress-bar-striped progress-bar-animated" style="width:37%"></div>
                  </div>
                </th>
              </tr>
              <tr></tr>
              <tr></tr>
              <tr></tr>
            </tbody>
          </table>
        </div>
      </div>

        <div id="tabla2" class="tabcontent">
            <div class="table-responsive2 my-3">
              <table class="table table table-striped table-hover my-2" style="background-color: #f5f6fa">
                <thead>
                  <tr>
                    <th>#</th>
                    <th>Name</th>
                    <th>Faculty</th>
                    <th></th>
                    <th>Progress of<br>donations</th>
                  </tr>
                </thead>
                <tbody>
                <tr>
                  <td>1</td>
                  <td>Mary</td>
                  <td>FCQI</td>
                  <td>
                    <center>
                      <button type="button" class="btn btn-primary btn-sm">Donar</button>
                    </center>
                  </td>
                  <th>
                    <div class="progress">
                      <div class="progress-bar progress-bar-striped progress-bar-animated" style="width:60%"></div>
                    </div>
                  </th>
                </tr>
                <tr>
                  <td>2</td>
                  <td>Mary</td>
                  <td>FCQI</td>
                  <td>
                    <center>
                      <button type="button" class="btn btn-primary btn-sm">Donar</button>
                    </center>
                  </td>
                  <th>
                    <div class="progress">
                      <div class="progress-bar progress-bar-striped progress-bar-animated" style="width:20%"></div>
                    </div>
                  </th>
                </tr>
                <tr>
                  <td>3</td>
                  <td>Mary</td>
                  <td>FCQI</td>
                  <td>
                    <center>
                      <button type="button" class="btn btn-primary btn-sm">Donar</button>
                    </center>
                  </td>
                  <th>
                    <div class="progress">
                      <div class="progress-bar progress-bar-striped progress-bar-animated" style="width:10%"></div>
                    </div>
                  </th>
                </tr>
                <tr>
                  <td>4</td>
                  <td>Mary</td>
                  <td>FCQI</td>
                  <td>
                    <center>
                      <button type="button" class="btn btn-primary btn-sm">Donar</button>
                    </center>
                  </td>
                  <th>
                    <div class="progress">
                      <div class="progress-bar progress-bar-striped progress-bar-animated" style="width:80%"></div>
                    </div>
                  </th>
                </tr>
                <tr>
                  <td>5</td>
                  <td>Mary</td>
                  <td>FCQI</td>
                  <td>
                    <center>
                      <button type="button" class="btn btn-primary btn-sm">Donar</button>
                    </center>
                  </td>
                  <th>
                    <div class="progress">
                      <div class="progress-bar progress-bar-striped progress-bar-animated" style="width:0%"></div>
                    </div>
                  </th>
                </tr>
                <tr>
                  <td>6</td>
                  <td>Mary</td>
                  <td>FCQI</td>
                  <td>
                    <center>
                      <button type="button" class="btn btn-primary btn-sm">Donar</button>
                    </center>
                  </td>
                  <th>
                    <div class="progress">
                      <div class="progress-bar progress-bar-striped progress-bar-animated" style="width:40%"></div>
                    </div>
                  </th>
                </tr>
                <tr>
                  <td>7</td>
                  <td>Mary</td>
                  <td>FCQI</td>
                  <td>
                    <center>
                      <button type="button" class="btn btn-primary btn-sm">Donar</button>
                    </center>
                  </td>
                  <th>
                    <div class="progress">
                      <div class="progress-bar progress-bar-striped progress-bar-animated" style="width:37%"></div>
                    </div>
                  </th>
                </tr>
                <tr></tr>
                <tr></tr>
                <tr></tr>
              </tbody>
            </table>
          </div>
        </div>

按钮的代码就是这个:

<div class="tab" >
                <button class="tablinks" onclick="openCity(event, 'tabla1')"><</button>
                <button class="tablinks2" onclick="openCity(event, 'tabla1')">1</button>
                <button class="tablinks3" onclick="openCity(event, 'tabla2')">2</button>
                <button class="tablinks4" onclick="openCity(event, 'tabla2')">3</button>
                <button class="tablinks" onclick="openCity(event, 'tabla2')">4</button>
                <button class="tablinks" onclick="openCity(event, 'tabla2')">></button>
            </div>

和javascript代码:

<script>
function openCity(evt, cityName) {
    var i, tabcontent, tablinks;
    tabcontent = document.getElementsByClassName("tabcontent");
    for (i = 0; i < tabcontent.length; i++) {
        tabcontent[i].style.display = "none";
    }
    tablinks = document.getElementsByClassName("tablinks");
    for (i = 0; i < tablinks.length; i++) {
        tablinks[i].className = tablinks[i].className.replace(" active", "");
    }
    document.getElementById(cityName).style.display = "block";
    evt.currentTarget.className += " active";
  }
  </script>

当我点击“2”按钮时,只有出现第二张表格,我才能做到这一点?这是我点击“2”按钮时在页面上显示的内容。 enter image description here

1 个答案:

答案 0 :(得分:1)

添加以下代码以隐藏除第一页加载以外的所有表:

document.querySelectorAll(".tabcontent").forEach(function(table, i){
  if(i >= 1){
    table.style.display = "none";
  }
});

工作代码段:

&#13;
&#13;
document.querySelectorAll(".tabcontent").forEach(function(table, i){
  if(i >= 1){
    table.style.display = "none";
  }
});
function openCity(evt, cityName) {
    var i, tabcontent, tablinks;
    tabcontent = document.getElementsByClassName("tabcontent");
    for (i = 0; i < tabcontent.length; i++) {
        tabcontent[i].style.display = "none";
    }
    tablinks = document.getElementsByClassName("tablinks");
    for (i = 0; i < tablinks.length; i++) {
        tablinks[i].className = tablinks[i].className.replace(" active", "");
    }
    document.getElementById(cityName).style.display = "block";
    evt.currentTarget.className += " active";
  }
&#13;
<div class="col-sm-8">
<div class="col-sm-8c col-md-12 bg-light">
<div class="col-sm-8 col-md-12">
<div id="tabla1" class="tabcontent">
  <div class="table-responsive my-3">
    <table class="table table table-striped table-hover my-2" style="background-color: #f5f6fa">
      <thead>
        <tr>
          <th>#</th>
          <th>Name</th>
          <th>Faculty</th>
          <th></th>
          <th>Progress of<br>donations</th>
        </tr>
      </thead>
      <tbody>
      <tr>
        <td>1</td>
        <td>Mary</td>
        <td>FCQI</td>
        <td>
          <center>
            <button type="button" class="btn btn-primary btn-sm">Donar</button>
          </center>
        </td>
        <th>
          <div class="progress">
            <div class="progress-bar progress-bar-striped progress-bar-animated" style="width:60%"></div>
          </div>
        </th>
      </tr>
      <tr>
        <td>2</td>
        <td>Mary</td>
        <td>FCQI</td>
        <td>
          <center>
            <button type="button" class="btn btn-primary btn-sm">Donar</button>
          </center>
        </td>
        <th>
          <div class="progress">
            <div class="progress-bar progress-bar-striped progress-bar-animated" style="width:20%"></div>
          </div>
        </th>
      </tr>
      <tr>
        <td>3</td>
        <td>Mariana</td>
        <td>FCQI</td>
        <td>
          <center>
            <button type="button" class="btn btn-primary btn-sm">Donar</button>
          </center>
        </td>
        <th>
          <div class="progress">
            <div class="progress-bar progress-bar-striped progress-bar-animated" style="width:10%"></div>
          </div>
        </th>
      </tr>
      <tr>
        <td>4</td>
        <td>Evelyn</td>
        <td>FCQI</td>
        <td>
          <center>
            <button type="button" class="btn btn-primary btn-sm">Donar</button>
          </center>
        </td>
        <th>
          <div class="progress">
            <div class="progress-bar progress-bar-striped progress-bar-animated" style="width:80%"></div>
          </div>
        </th>
      </tr>
      <tr>
        <td>5</td>
        <td>Mary</td>
        <td>FCQI</td>
        <td>
          <center>
            <button type="button" class="btn btn-primary btn-sm">Donar</button>
          </center>
        </td>
        <th>
          <div class="progress">
            <div class="progress-bar progress-bar-striped progress-bar-animated" style="width:0%"></div>
          </div>
        </th>
      </tr>
      <tr>
        <td>6</td>
        <td>Mary</td>
        <td>FCQI</td>
        <td>
          <center>
            <button type="button" class="btn btn-primary btn-sm">Donar</button>
          </center>
        </td>
        <th>
          <div class="progress">
            <div class="progress-bar progress-bar-striped progress-bar-animated" style="width:40%"></div>
          </div>
        </th>
      </tr>
      <tr>
        <td>7</td>
        <td>Sebastian</td>
        <td>FCQI</td>
        <td>
          <center>
            <button type="button" class="btn btn-primary btn-sm">Donar</button>
          </center>
        </td>
        <th>
          <div class="progress">
            <div class="progress-bar progress-bar-striped progress-bar-animated" style="width:37%"></div>
          </div>
        </th>
      </tr>
      <tr></tr>
      <tr></tr>
      <tr></tr>
    </tbody>
  </table>
</div>
</div>

<div id="tabla2" class="tabcontent">
    <div class="table-responsive2 my-3">
      <table class="table table table-striped table-hover my-2" style="background-color: #f5f6fa">
        <thead>
          <tr>
            <th>#</th>
            <th>Name</th>
            <th>Faculty</th>
            <th></th>
            <th>Progress of<br>donations</th>
          </tr>
        </thead>
        <tbody>
        <tr>
          <td>1</td>
          <td>Mary</td>
          <td>FCQI</td>
          <td>
            <center>
              <button type="button" class="btn btn-primary btn-sm">Donar</button>
            </center>
          </td>
          <th>
            <div class="progress">
              <div class="progress-bar progress-bar-striped progress-bar-animated" style="width:60%"></div>
            </div>
          </th>
        </tr>
        <tr>
          <td>2</td>
          <td>Mary</td>
          <td>FCQI</td>
          <td>
            <center>
              <button type="button" class="btn btn-primary btn-sm">Donar</button>
            </center>
          </td>
          <th>
            <div class="progress">
              <div class="progress-bar progress-bar-striped progress-bar-animated" style="width:20%"></div>
            </div>
          </th>
        </tr>
        <tr>
          <td>3</td>
          <td>Mary</td>
          <td>FCQI</td>
          <td>
            <center>
              <button type="button" class="btn btn-primary btn-sm">Donar</button>
            </center>
          </td>
          <th>
            <div class="progress">
              <div class="progress-bar progress-bar-striped progress-bar-animated" style="width:10%"></div>
            </div>
          </th>
        </tr>
        <tr>
          <td>4</td>
          <td>Mary</td>
          <td>FCQI</td>
          <td>
            <center>
              <button type="button" class="btn btn-primary btn-sm">Donar</button>
            </center>
          </td>
          <th>
            <div class="progress">
              <div class="progress-bar progress-bar-striped progress-bar-animated" style="width:80%"></div>
            </div>
          </th>
        </tr>
        <tr>
          <td>5</td>
          <td>Mary</td>
          <td>FCQI</td>
          <td>
            <center>
              <button type="button" class="btn btn-primary btn-sm">Donar</button>
            </center>
          </td>
          <th>
            <div class="progress">
              <div class="progress-bar progress-bar-striped progress-bar-animated" style="width:0%"></div>
            </div>
          </th>
        </tr>
        <tr>
          <td>6</td>
          <td>Mary</td>
          <td>FCQI</td>
          <td>
            <center>
              <button type="button" class="btn btn-primary btn-sm">Donar</button>
            </center>
          </td>
          <th>
            <div class="progress">
              <div class="progress-bar progress-bar-striped progress-bar-animated" style="width:40%"></div>
            </div>
          </th>
        </tr>
        <tr>
          <td>7</td>
          <td>Mary</td>
          <td>FCQI</td>
          <td>
            <center>
              <button type="button" class="btn btn-primary btn-sm">Donar</button>
            </center>
          </td>
          <th>
            <div class="progress">
              <div class="progress-bar progress-bar-striped progress-bar-animated" style="width:37%"></div>
            </div>
          </th>
        </tr>
        <tr></tr>
        <tr></tr>
        <tr></tr>
      </tbody>
    </table>
  </div>
</div>

<div class="tab" >
  <button class="tablinks" onclick="openCity(event, 'tabla1')"><</button>
  <button class="tablinks2" onclick="openCity(event, 'tabla1')">1</button>
  <button class="tablinks3" onclick="openCity(event, 'tabla2')">2</button>
  <button class="tablinks4" onclick="openCity(event, 'tabla2')">3</button>
  <button class="tablinks" onclick="openCity(event, 'tabla2')">4</button>
  <button class="tablinks" onclick="openCity(event, 'tabla2')">></button>
</div>
&#13;
&#13;
&#13;