Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/412.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在javascript中使单元格表的href链接可单击?_Javascript_Html_Href - Fatal编程技术网

如何在javascript中使单元格表的href链接可单击?

如何在javascript中使单元格表的href链接可单击?,javascript,html,href,Javascript,Html,Href,我有一个表来显示API中的所有数据。我的代码如下所示: <div class="table-responsive"> <h1>List Existing Node</h1> <br/> <table class="table table-bordered table-striped" id="node_table"> <tr>

我有一个表来显示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 纬度 经度 位置 $(文档).ready(函数(){ $.getJSON(“/api/node/”,函数(数据){ 变量节点_数据=“”; $。每个(数据、函数(键、值){ 节点_数据+=''; node_data+=''+value.node_id; 节点_数据+=''+值。纬度; 节点_数据+=''+值.经度; 节点_数据+=''+值.location; 节点_数据+=''; }); $('#node_table')。追加(node_数据); 控制台日志(数据); }); });
问题是我希望可以使用href链接单击节点Id列中的所有单元格表

例如,当我单击节点Id列中的一个单元格(例如:节点1、节点2或节点n)时,页面将重定向到

我该怎么做?

$('node_table')。在('click','tr',function()上{
$('#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);

});
var href=$(this.data('href'); window.location.href=href; }) $.getJSON(“/api/node/”,函数(数据){ 变量节点_数据=“”; $。每个(数据、函数(键、值){ 节点_数据+=''; node_data+=''+value.node_id; 节点_数据+=''+值。纬度; 节点_数据+=''+值.经度; 节点_数据+=''+值.location; 节点_数据+=''; }); $('#node_table')。追加(node_数据); 控制台日志(数据); });
您可以执行以下操作:

<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>

列出现有节点

节点Id 纬度 经度 位置 $(文档).ready(函数(){ $.getJSON(“/api/node/”,函数(数据){ 变量节点_数据=“”; $。每个(数据、函数(键、值){ 节点_数据+=''; 节点_数据+=''; 节点_数据+=''+值。纬度; 节点_数据+=''+值.经度; 节点_数据+=''+值.location; 节点_数据+=''; }); $('#node_table')。追加(node_数据); 控制台日志(数据); }); });
修改
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);
});
$.getJSON('/api/node/',函数(数据){
变量节点_数据=“”;
var href=https://facebook.com';
$。每个(数据、函数(键、值){
节点_数据+='';
节点_数据+='';
节点_数据+=''+值.纬度+'';
节点_data+=''+value.longitude+'';
节点_数据+=''+值.location+'';
节点_数据+='';
});
$('#node_table')。追加(node_数据);
});
节点数据+=''