javascript - 未捕获的类型错误:$(...).jstree 不是函数
问题描述
我正在尝试创建 jstree,但我希望它在几秒钟后加载,我有一个可以工作的代码,但是如果我将它放在另一个函数中,例如 setTimeOut,我会收到一个错误 Uncaught TypeError: $ (...)。Jstree 不是函数
这是我的脚本:
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.2.1/jstree.min.js"></script>
<link rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.2.1/themes/default/style.min.css" />
如果我输入这段代码,那么一切都很好:
<script>
let cld;
let rr = [{ text: "1001", id: "190", type: "1", children: true }
, { text: "1002", id: "192", type: "1", children: true }
, { text: "1004", id: "203", type: "3", children: true }
, { text: "8002", id: "308", type: "1", children: true }
, { text: "1007", id: "314", type: "1", children: true }
, { text: "9001", id: "316", type: "3", children: true }
, { text: "6513", id: "331", type: "3", children: true }];
$('#tree').jstree({
'core': {
'data': function (node, cb) {
if (node.id === "#") {
cb(rr);
}
else {
cld = [{ text: "9001", id: "316", type: "3", children: true }
, { text: "6513", id: "331", type: "3", children: true }];
cb(cld);
}
}
}
});
</script>
但是,如果我将此代码放入函数 setTimeOut 中,例如:
<script>
let rr = [{ text: "1001", id: "190", type: "1", children: true }
, { text: "1002", id: "192", type: "1", children: true }
, { text: "1004", id: "203", type: "3", children: true }
, { text: "8002", id: "308", type: "1", children: true }
, { text: "1007", id: "314", type: "1", children: true }
, { text: "9001", id: "316", type: "3", children: true }
, { text: "6513", id: "331", type: "3", children: true }];
setTimeout(function () {
$('#tree').jstree({
'core': {
'data': function (node, cb) {
if (node.id === "#") {
cb(rr);
}
else {
cld = [{ text: "9001", id: "316", type: "3", children: true }
, { text: "6513", id: "331", type: "3", children: true }];
cb(cld);
}
}
}
});
}, 1000);
</script>
我得到错误:
Uncaught TypeError: $(...).jstree is not a function
我的代码有什么问题?
谢谢
解决方案
推荐阅读
- c++ - 我在图像转换算法中做错了什么?
- android - 在 Android Studio 的组按钮中选择了两个单选按钮
- elasticsearch - 如何在具有不同数据的一台服务器上设置多个弹性搜索实例?
- android-studio - 为什么我的导航视图会覆盖整个 FrameLayout 的内容
- mongodb - 如何使用 Mongo 和多个字段进行过滤
- php - PHP 注意:试图访问 bool 类型值的数组偏移量
- laravel - 如何在 laravel 中过滤集合?
- java - 如何使用 Mockito 模拟此方法
- qt - 是否可以在 QML 页面中使用 QML Singleton 作为标题项?
- c# - 如何使用 Xamarin 中的开关更改页面背景颜色