首页 > 解决方案 > 未捕获的类型错误:$(...).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

我的代码有什么问题?

谢谢

标签: javascriptjstree

解决方案


推荐阅读