单击节点时打开分支?

我在这里遇到了jsTree 。 到目前为止,它可以工作,我可以使用[+]图标浏览和扩展节点,并在单击节点时打开页面,但我还是希望它在有人点击某个节点时展开所有直接节点。

我看了看周围至少2个小时但是找不到任何东西。 官方网站不是很有帮助,因为他们没有足够的例子,而且没有很好的记录。 看看这个,但对我来说也不起作用: http : //luban.danse.us/jazzclub/javascripts/jquery/jsTree/reference/_examples/2_operations.html

我甚至没有在firebug中收到错误消息

所以这是我的代码现在看起来如何,树初始化:

$(function () { $("#jstree").jstree({ .... 

单击节点触发的function

 .delegate("a","click", function (e) { //click on node var page_id = $(this).parent().attr("page_id"); var idn = $(this).parent().attr("id").split("_")[1]; /* dosnt seem to work either... $(this).jstree("openNode", $("#node_"+idn)); $(this).jstree("openNode", "#node_"+idn); */ page = "index.php?page_id="+page_id; //location.href = page; }) 

.bind也没有工作:

 $(this).bind("open_node.jstree", function (event, data) { if((data.inst._get_parent(data.rslt.obj)).length) { data.inst._get_parent(data.rslt.obj).open_node(this, false); } }) 

谁有人看到我在这里失踪了……?

您需要绑定到select_node.jstree并在触发时在树实例上调用toggle_node:

对于jsTree版本<3.0:

 $("#your_tree").bind("select_node.jstree", function(event, data) { // data.inst is the tree object, and data.rslt.obj is the node return data.inst.toggle_node(data.rslt.obj); }); 

对于jsTree版本> = 3.0

 $("#your_tree").bind("select_node.jstree", function (e, data) { return data.instance.toggle_node(data.node); }); 

使用更新版本的jsTree(根据jsTree.js 3.0.0),我不得不将@justind提供的代码更改为有效:

 $("#jstree").bind("select_node.jstree", function (e, data) { return data.instance.toggle_node(data.node); }); 

我用这个(casoUso是页面链接,fInvocaCasoUso是一个函数来进行调用)。

  $("#demo1").bind("select_node.jstree", function (e, data) { if (data.rslt.obj.attr("casoUso")!=undefined) { fInvocaCasoUso(data.rslt.obj.attr("casoUso")); } else { $("#demo1").jstree("toggle_node",data.rslt.obj); } }); 

如果节点有链接,则打开,否则打开子树。 无论如何,您应该能够组合“if”的两侧来打开分支并执行您的链接。 也许执行:

  $("#demo1").jstree("toggle_node",data.rslt.obj); fInvocaCasoUso(data.rslt.obj.attr("casoUso")); 

会这样做……