javascript - 在不反转顶部的情况下反转 HTML 表格的行

标签 javascript html css

我有一个基本上是这样的表:

a1 a2 a3

b1 b2 b3

c1 c2 c3

我想把它反转成这个样子

c1 c2 c3

b1 b2 b3

a1 a2 a3

每当按下“点击我”按钮时

<table class='standardtable'>
<tr>
   <th>Event</th>
   <th>Group</th>
   <th>Course</th>
   <th>Due Date</th>
   <th>Due In/<span class='red'>Late By</span></th>
  <button onclick="myFunction()">Click me</button>
</tr>
<td id='others'>
<?php
echo $html->tableCells($evalUpcoming);
?>
<?php if (empty($evalUpcoming)):?>
<tr><td colspan="5" align="center"><b> No peer evaluations due at this time </b></td></tr>
<?php endif; ?>
</td>

很明显第一行是标题,其他的是内容。我尝试使用 td 分隔第一行和另一行,但它似乎不起作用:

<table class='standardtable'>
<tr>
   <th>Event</th>
   <th>Group</th>
   <th>Course</th>
   <th>Due Date</th>
   <th>Due In/<span class='red'>Late By</span></th>
  <button onclick="myFunction()">Click me</button>
</tr>
<td id='others'>
<?php
echo $html->tableCells($evalUpcoming);
?>
<?php if (empty($evalUpcoming)):?>
<tr><td colspan="5" align="center"><b> No peer evaluations due at this time </b></td></tr>
<?php endif; ?>
</td>
<script>
    function myFunction(){
        var table = document.getElementById('others');

        for (var i = 0; i < table.length; i++) {
            var rows = table[i].rows;
            for (var j = 0; j < rows.length; j++) {
                    rows[j].parentNode.insertBefore(rows[rows.length-1], rows[j]);
            }
        }
        }

    </script>

谁能告诉我如何在不翻转第一行的情况下翻转这张表?

最佳答案

像这样构建你的表:

<table class='standardtable'>
<thead> <!-- use thead to separate headercontent -->
  <tr>
     <th>Event</th>
     <th>Group</th>
     <th>Course</th>
     <th>Due Date</th>
     <th>Due In/<span class='red'>Late By</span></th>
    <button onclick="myFunction()">Click me</button>
  </tr>
</thead>
<tbody> <!-- use tbody to separate tablecontent-->
  <?php
  echo $html->tableCells($evalUpcoming);
  ?>
  <?php if (empty($evalUpcoming)):?>
  <tr><td colspan="5" align="center"><b> No peer evaluations due at this time </b></td></tr>
  <?php endif; ?>
</tbody>

关于javascript - 在不反转顶部的情况下反转 HTML 表格的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34043353/

相关文章:

javascript - 仅保存 HTML Canvas 的特定部分

javascript - SAPUI5 - 对列表项进行分组,而不按升序或降序排序

javascript - 如何使用 javascript 将我的域名字母大写?

javascript - 切换 Div 的内容 onclick

javascript - Trello 保持更新?

javascript - ReactJS material-ui TextField 应用css

javascript - 记住下一页的 slug

javascript - 通过单击按钮添加行

php - Xpath选择注释代码

css - 我的 CSS 样式的某些部分未应用。我该如何解决这个问题?