我正在尝试为我的元素使用 jQuery 的 Datatable JS this链接。
我从同一来源下载了完整的库。包中给出的所有示例似乎都可以正常工作,但是当我尝试将它们合并到我的 WebForms
中时,CSS、JS 根本无法工作。
这是我所做的:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<link href="css/jquery.dataTables.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form id="form1" runat="server">
<div>
<table id="myTable" class="display" cellspacing="0" width="100%">
<thead>
<tr>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Age</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Age</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</tfoot>
<tbody>
<!-- table body -->
</tbody>
</table>
</div>
<script src="js/jquery.dataTables.js" type="text/javascript"></script>
<script src="js/jquery.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
$('#myTable').DataTable();
});
</script>
</form>
</body>
</html>
我的解决方案中的 JS 和 CSS 文件结构如下所示:
我已经添加了所有必要的 JS 和 CSS 引用,如手册中所示。渲染仍然不符合预期。没有CSS,连JS都不行。
我还在控制台中收到以下错误:
ReferenceError: jQuery is not defined
TypeError: $(...).DataTable is not a function
我还没有在这里绑定(bind)任何动态数据(比如在中继器内),但它仍然无法正常工作。
有人可以指导我解决这个问题吗?
最佳答案
原因
此错误可能有多种原因。
- 缺少 jQuery DataTables 库。
- jQuery 库在 jQuery DataTables 之后加载。
- 加载了多个版本的 jQuery 库。
解决方案
在 jQuery DataTables 之前包含唯一版本的 jQuery 库版本 1.7 或更新版本。
例如:
<script src="js/jquery.min.js" type="text/javascript"></script>
<script src="js/jquery.dataTables.min.js" type="text/javascript"></script>
参见 jQuery DataTables: Common JavaScript console errors有关此错误和其他常见控制台错误的更多信息。
关于javascript - TypeError : $(. ..).DataTable 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31227844/