javascript - 如何在javascript中使单元格表的href链接可点击?
问题描述
我有一个表格来显示我的 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_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);
});
推荐阅读
- php - Symfony OneToOne 关系未在用户实体中获取数据
- reactjs - 为什么在使用 KeyboardAvoidView 时会出现“不变违规”错误?
- r - 用百分比重新排序 ggplot2 中的条形图
- java - HashMap containsKey 对于相同的字符串总是返回 false
- ubuntu - 无法更新 plesk 面板
- r - 在 R 代码中将 NaN 隐藏为零
- javascript - 如何在 JavaScript 中检测“不同的对象类型”?(数组 vs Json vs Date 等...)
- javascript - javascript中bind(this)的复合用法
- php - PHPUnit 没有看到固定装置的变化
- reactjs - 如何在 React Native 和 Reflux 中进行多个 API 调用