javascript - 如何在 Express 中使用 jsTree

标签 javascript node.js express jstree

我已经安装了jsTree在我的 bower_components 中(使用 this post 中的指南),我的 Jade 模板看起来像这样 -

doctype html
html
    head
        title= title
        script(src='/bower_components/jstree/dist/jstree.min.js')
        script(src='/bower_components/jquery/dist/jquery.min.js')
        link(rel='stylesheet', href='/stylesheets/style.css')
        link(rel='stylesheet', href='/bower_components/jstree/dist/themes/default/style.css')
        script(src='/javascripts/custom/mytree.js')
    body
        #mytreediv
            ul
                li First item
                li Second item

脚本mytree.js是我编写的脚本,我在其中使用jsTree初始化mytreediv div元素。脚本看起来像这样 -

alert('Test1');
var myfunc = function () {
    alert('Test2');
    $('#mytreediv').jstree();
};
myfunc();

两个 alert 框确实出现了,所以我非常确定脚本已正确链接到 HTML。但是,div 元素看起来并没有转换为 jsTree。

我在这里缺少什么?

最佳答案

你必须等待 DOM 加载,否则 jQuery 将无法找到该元素:

$(document).ready(function () { 
    /* your code */ 
})

关于javascript - 如何在 Express 中使用 jsTree,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36064990/

相关文章:

javascript - 如何在处理 HTTP 请求之前将 csv 文件同步加载到内存中

javascript - req.body 未填充表单数据

node.js - Express 如何知道运行 "server.js"?

javascript - JS 检查深层对象属性是否存在

javascript - Expressjs 正文解析器 : how to solve "TypeError: Cannot read property ' name' of undefined"- already reviewed all previouse ansers

javascript - jQuery id 不产生文本表示

node.js - nodejs将html文件发送到客户端

node.js - 快速更新 JWT 作为响应

javascript - HTML 选择 onChange 不起作用

javascript - 连续事件不适用于从上一个事件创建的元素