【问题标题】:How to make href link clickable for cell table in javascript?如何在javascript中使单元格表的href链接可点击?
【发布时间】:2020-05-23 14:41:25
【问题描述】:

我有一个表格来显示我的 API 中的所有数据。我的代码是这样的:

<div class="table-responsive">
        <h1>List Existing Node</h1>
        <br/>
        <table class="table table-bordered table-striped" id="node_table">
            <tr>
                <th>Node Id</th>
                <th>Latitude</th>
                <th>Longitude</th>
                <th>Location</th>
            </tr>
        </table>
    </div>
<script>
    $(document).ready(function() {
        $.getJSON("/api/node/", function(data){
            var node_data = '';
            $.each(data, function(key, value){
                node_data += '<tr>';
                node_data += '<td>'+value.node_id;
                node_data += '<td>'+value.latitude;
                node_data += '<td>'+value.longitude;
                node_data += '<td>'+value.location;
                node_data += '</tr>';
            });
            $('#node_table').append(node_data);
            console.log(data);

        });
    });<script>

问题是我希望节点 ID 列中的所有单元格表都可以使用 href 链接单击。

例如,当我单击节点 ID 列中的单元格(例如:节点 1 或节点 2 或节点 n)时,页面将重定向到 https://facebook.com

我该怎么做?

【问题讨论】:

  • node_data += '&lt;td&gt;&lt;a href="https://facebook.com"&gt;'+value.node_id+'&lt;/a&gt;&lt;/td&gt;'

标签: javascript html href


【解决方案1】:

修改ready回调如下:

$.getJSON( '/api/node/', function(data){
    var node_data = '';
    var href = 'https://facebook.com';

    $.each(data, function(key, value){
        node_data += '<tr>';
        node_data += '<td> <a href="' + href + '">' + value.node_id + '</a></td>';
        node_data += '<td>' + value.latitude + '</td>';
        node_data += '<td>' + value.longitude + '</td>';
        node_data += '<td>' + value.location + '</td>';
        node_data += '</tr>';
    });
    $('#node_table').append(node_data);
});

【讨论】:

    【解决方案2】:
    $('#node_table').on('click', 'tr', function() {
        var href = $(this).data('href');
        window.location.href = href;
    })
    
    $.getJSON("/api/node/", function(data){
        var node_data = '';
        $.each(data, function(key, value){
            node_data += '<tr data-href="your link here">';
            node_data += '<td>'+value.node_id;
            node_data += '<td>'+value.latitude;
            node_data += '<td>'+value.longitude;
            node_data += '<td>'+value.location;
            node_data += '</tr>';
        });
        $('#node_table').append(node_data);
        console.log(data);
    
    });
    

    【讨论】:

      【解决方案3】:

      你可以这样做:

      <div class="table-responsive">
          <h1>List Existing Node</h1>
          <br/>
          <table class="table table-bordered table-striped" id="node_table">
              <tr>
                  <th>Node Id</th>
                  <th>Latitude</th>
                  <th>Longitude</th>
                  <th>Location</th>
              </tr>
          </table>
      </div>
      <script>
          $(document).ready(function() {
              $.getJSON("/api/node/", function(data){
                  var node_data = '';
                  $.each(data, function(key, value){
                      node_data += '<tr>';
                      node_data += '<td> <a href="https://facebook.com/">'+value.node_id+'</a></td>';
                      node_data += '<td>'+value.latitude;
                      node_data += '<td>'+value.longitude;
                      node_data += '<td>'+value.location;
                      node_data += '</tr>';
                  });
                  $('#node_table').append(node_data);
                  console.log(data);
      
              });
          });<script>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-01-22
        • 2021-06-20
        • 2010-11-27
        • 2014-06-25
        • 2011-02-01
        • 1970-01-01
        • 2016-05-17
        • 2012-10-06
        相关资源
        最近更新 更多