javascript - TypeError : $(. ..).DataTable 不是函数

标签 javascript jquery css asp.net datatables

我正在尝试为我的元素使用 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 文件结构如下所示:

File structure of JS and CSS in solution

我已经添加了所有必要的 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/

相关文章:

jquery - IE 中的嵌套样式覆盖了我在 CSS Sprite 上的相同名称

javascript - 以编程方式检查网站是否被 ISP 阻止

javascript - 具有单选按钮类型行为的复选框

javascript - 将 jquery 作为参数传递给可自动执行的函数

javascript - 如何使用 jQuery 检测页面是否已完全呈现?

css - 如何:hover but :not for :first-child of element?

javascript - 从上午 9 点到下午 12 点,使用触发器每 30 分钟运行一次脚本

javascript - 如何在向下滚动时实现 float div

jquery - 为什么这段代码不循环?

javascript - Jquery Mobile (JQM) 1.4.5 Page Min-Height 将页面高度降低到小于 100%