Javascript 类型错误:$(...)DataTable 不是函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31632294/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TypeError: $(...)DataTable is not a function
提问by SML
I am trying to use the jquery plugin data tables, but I can't seem to load the function. I keep getting this error:
我正在尝试使用 jquery 插件数据表,但我似乎无法加载该功能。我不断收到此错误:
Uncaught TypeError: $(...).DataTable is not a function
(anonymous function) @ index.php:167
m.Callbacks.j @ jquery.min.js:2
m.Callbacks.k.fireWith @ jquery.min.js:2
m.extend.ready @ jquery.min.js:2
J @ jquery.min.js:2
Below is my JS code:
下面是我的JS代码:
$(document).ready(function(){
$('table#tableID').DataTable({
paging: true
});
});
I am using jQuery V. 1.11.1 I tried to look around for solution, and saw people talking about jQuery not being loaded. I am running other jQuery functions on the same page successfully. This is also the only .ready function on this page. We can tell that jQuery is present, as when the document is ready, it executes the function. I also tried to place the imports of the js and css file in multiple places, as suggested, but did not work. Does anyone have any clue how to fix this?
我正在使用 jQuery V。1.11.1 我试图环顾四周寻找解决方案,看到人们谈论 jQuery 没有被加载。我在同一页面上成功运行其他 jQuery 函数。这也是此页面上唯一的 .ready 函数。我们可以看出 jQuery 存在,因为当文档准备好时,它会执行该函数。我还尝试按照建议将 js 和 css 文件的导入放置在多个位置,但没有奏效。有谁知道如何解决这个问题?
EDITS:
编辑:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="/css/jquery.dataTables.css">
<script type="text/javascript" charset="utf8" src="/js/jquery.dataTables.js"></script>
回答by charlietfl
Order of scripts is important when they are dependent on libraries or other scripts.
当脚本依赖于库或其他脚本时,脚本的顺序很重要。
Any jQuery related code needs to be included after jQuery.js... that means plugins and any code you write that uses jQuery. Similarly any code you write that uses a plugin must have the plugin loaded before your code
任何与 jQuery 相关的代码都需要包含在 jQuery.js 之后……这意味着插件和您编写的任何使用 jQuery 的代码。同样,您编写的任何使用插件的代码都必须在您的代码之前加载插件
Simply switch the order so jQuery.js loads before dataTables.js
只需切换顺序,使 jQuery.js 在 dataTables.js 之前加载
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript" charset="utf8" src="/js/jquery.dataTables.js"></script>
Also make sure you only ever include jQuery once in a page...not once per plugin as some occasionally do
还要确保你只在一个页面中包含一次 jQuery ......而不是每个插件一次,因为有些人偶尔会这样做
回答by Spencer Wieczorek
This is because you are loading the jQuery library before loading jQuery itself. jQuery needs to be loaded before you load the library, you can do that by including the <script>
for jQuery before your library:
这是因为您在加载 jQuery 本身之前加载了 jQuery 库。需要在加载库之前加载 jQuery,您可以通过<script>
在库之前包含for jQuery 来实现:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript" charset="utf8" src="/js/jquery.dataTables.js"></script>