javascript - 创建交互式表格 - HTML、CSS、JavaScript

标签 javascript html css

我的目标是让我的表格具有交互性,并且所有代码都在一个文档中。我不想在主文档之外引用 JavaScript 或 CSS,这可能是我的问题的根源。

单击按钮时 JavaScript 似乎不起作用,我不确定发生了什么。

这是我的代码:

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css"/>

    <script>

      $(function(){
        var TABLE = $("table");

        $(".table-add").click(function() {
          console.log('adding');

          var clone = TABLE
            .find("tr.hide")
            .clone(true)
            .removeClass("hide table-line");

          TABLE.append(clone);
        });

        $(".table-remove").click(function() {
          $(this)
            .parents("tr")
            .detach();
        });

        $(".table-up").click(function() {
          var $row = $(this).parents("tr");
          if ($row.index() === 1) return;
          $row.prev().before($row.get(0));
        });

        $(".table-down").click(function() {
          var $row = $(this).parents("tr");
          $row.next().after($row.get(0));
        });
      })
    </script>

       <style>
            @import "compass/css3";

                .table-editable {
                  position: relative;

                  .glyphicon {
                    font-size: 20px;
                  }
                }

                .table-remove {
                  color: #700;
                  cursor: pointer;

                }

                .table-up, .table-down {
                  color: #007;
                  cursor: pointer;

                }

                .table-add {
                  color: #070;
                  cursor: pointer;
                  position: absolute;
                  top: 8px;
                  right: 0;
                }
          </style>


  <body>
    <div class="container">
    <h1>HTML5 Editable Table</h1>


    <div id="table" class="table-editable">
      <span class="table-add glyphicon glyphicon-plus"></span>
      <table class="table">
        <tr>
          <th>Outcomes</th>
          <th>Implementation/Sensors</th>
          <th>Alert Type</th>
          <th>Responder/Contact Info</th>
        </tr>
        <tr>
          <td contenteditable="true">Outcome</td>
          <td contenteditable="true">Sensor</td>
          <td contenteditable="true">Alert</td>
          <td contenteditable="true">Contact</td>
          <td>
            <span class="table-remove glyphicon glyphicon-remove"></span>
          </td>
          <td>
            <span class="table-up glyphicon glyphicon-arrow-up"></span>
            <span class="table-down glyphicon glyphicon-arrow-down"></span>
          </td>
        </tr>
        <!-- This is our clonable table line -->
        <tr class="hide">
          <td contenteditable="true">Outcome</td>
          <td contenteditable="true">Sensor</td>
          <td contenteditable="true">Alert</td>
          <td contenteditable="true">Contact</td>
          <td>
            <span class="table-remove glyphicon glyphicon-remove"></span>
          </td>
          <td>
            <span class="table-up glyphicon glyphicon-arrow-up"></span>
            <span class="table-down glyphicon glyphicon-arrow-down"></span>
          </td>
        </tr>
      </table>
    </div>
  </div>

  </body>

最佳答案

需要向标签添加功能

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css"/>

    <script>

      $(function(){
        var TABLE = $("table");

        $(".table-add").click(function() {
          console.log('adding');

          var clone = TABLE
            .find("tr.hide")
            .clone(true)
            .removeClass("hide table-line");

          TABLE.append(clone);
        });

        $(".table-remove").click(function() {
          $(this)
            .parents("tr")
            .detach();
        });

        $(".table-up").click(function() {
          var $row = $(this).parents("tr");
          if ($row.index() === 1) return;
          $row.prev().before($row.get(0));
        });

        $(".table-down").click(function() {
          var $row = $(this).parents("tr");
          $row.next().after($row.get(0));
        });
      })
    </script>

       <style>
            @import "compass/css3";

                .table-editable {
                  position: relative;

                  .glyphicon {
                    font-size: 20px;
                  }
                }

                .table-remove {
                  color: #700;
                  cursor: pointer;

                }

                .table-up, .table-down {
                  color: #007;
                  cursor: pointer;

                }

                .table-add {
                  color: #070;
                  cursor: pointer;
                  position: absolute;
                  top: 8px;
                  right: 0;
                }
          </style>


  <body>
    <div class="container">
    <h1>HTML5 Editable Table</h1>


    <div id="table" class="table-editable">
      <span class="table-add glyphicon glyphicon-plus"></span>
      <table class="table">
        <tr>
          <th>Outcomes</th>
          <th>Implementation/Sensors</th>
          <th>Alert Type</th>
          <th>Responder/Contact Info</th>
        </tr>
        <tr>
          <td contenteditable="true">Outcome</td>
          <td contenteditable="true">Sensor</td>
          <td contenteditable="true">Alert</td>
          <td contenteditable="true">Contact</td>
          <td>
            <span class="table-remove glyphicon glyphicon-remove"></span>
          </td>
          <td>
            <span class="table-up glyphicon glyphicon-arrow-up"></span>
            <span class="table-down glyphicon glyphicon-arrow-down"></span>
          </td>
        </tr>
        <!-- This is our clonable table line -->
        <tr class="hide">
          <td contenteditable="true">Outcome</td>
          <td contenteditable="true">Sensor</td>
          <td contenteditable="true">Alert</td>
          <td contenteditable="true">Contact</td>
          <td>
            <span class="table-remove glyphicon glyphicon-remove"></span>
          </td>
          <td>
            <span class="table-up glyphicon glyphicon-arrow-up"></span>
            <span class="table-down glyphicon glyphicon-arrow-down"></span>
          </td>
        </tr>
      </table>
    </div>
  </div>

  </body>

关于javascript - 创建交互式表格 - HTML、CSS、JavaScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60643587/

相关文章:

javascript - 为什么 redux-loop 操作会跳过所有中间件?

javascript - 表中的 TD 总和

php - PHP 和 JavaScript 中的字符串压缩

javascript - 无法使用 ESRI Javascript API 和 html2canvasa 打印 map div

html - 如何将我的内容对齐到中心?

javascript - 如何从 bigquery API 查询 bigquery View

html - 如何将具有定义宽度和高度的 div 定位在另一个 div 的中心?

javascript - 从外部文件加载引导表

html - Bootstrap 导航折叠

javascript - 使用 ng-hide/ng-show 和函数显示更多,显示更少