[英]Loading a table from a separate HTML
说我有两个文件:
index.html
table.html
其中table.html
保存我的表(例如以下内容):
<table border="1" class="dataframe" id="my_table">
<thead>
<tr style="text-align: right;">
<th>school</th>
<th>county</th>
<th>zipcode</th>
</tr>
</thead>
<tbody>
<tr>
<td>XX</td>
<td>YY</td>
<td>ZZ</td>
</tr>
<tr>
<td>XX</td>
<td>YY</td>
<td>ZZ</td>
</tr>
</tbody>
</table>
和index.html
只是有datatables
代码,例如
<html>
<head>
<link rel="stylesheet" type="text/css" href="https://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/css/jquery.dataTables.css">
</head>
<body>
<script type="text/javascript" charset="utf8" src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
<script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.10.9/js/jquery.dataTables.min.js"></script>
<script>
$(function(){
$("#my_table").dataTable();
})
</script>
</body>
</html>
上面的方法不起作用,因为index.html
找不到引用my_table
。 我如何使其“嵌入”(或使其“意识到”) table.html
?
使用iframe在首页中呈现表格页:
主HTML
<!doctype html>
<html lang="en">
<head>
<title></title>
</head>
<body>
<iframe src="tablepage.html"></iframe>
</body>
</html>
表格页面HTML
<!doctype html>
<html>
<head>
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.9/css/jquery.dataTables.min.css" />
</head>
<body>
<table border="1" class="dataframe" id="my_table">
<thead>
<tr style="text-align: right;">
<th>school</th>
<th>county</th>
<th>zipcode</th>
</tr>
</thead>
<tbody>
<tr>
<td>XX</td>
<td>YY</td>
<td>ZZ</td>
</tr>
<tr>
<td>XX</td>
<td>YY</td>
<td>ZZ</td>
</tr>
</tbody>
</table>
<script type="text/javascript" src="jQuery.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/1.10.9/js/jquery.dataTables.min.js"></script>
<script src="theJS.js"></script>
</body>
</html>
的JavaScript
$(document).ready(function () {
$('#my_table').DataTable();
});
您可以使用load()
函数插入HTML页面
<script>
$(function(){
$("#table").load("table.html");
});
</script>
Index.html
<html>
<head>
<link rel="stylesheet" type="text/css" href="https://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/css/jquery.dataTables.css">
<script type="text/javascript" charset="utf8" src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
<script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.10.9/js/jquery.dataTables.min.js"></script>
<script>
$(function(){
$("#table").load("table.html");
});
$(function(){
$("#my_table").dataTable();
});
</script>
</head>
<body>
<div id="table"></div>
</body>
</html>
和table.html
<table border="1" class="dataframe" id="my_table">
<thead>
<tr style="text-align: right;">
<th>school</th>
<th>county</th>
<th>zipcode</th>
</tr>
</thead>
<tbody>
<tr>
<td>XX</td>
<td>YY</td>
<td>ZZ</td>
</tr>
<tr>
<td>XX</td>
<td>YY</td>
<td>ZZ</td>
</tr>
</tbody>
</table>
您可以使用jQuery.get() (请参见上一个示例)从服务器获取第二个文件,然后将HTML代码附加到当前页面中,然后就可以对其进行修改。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.