如何在jstree中获取叶子节点,以便在使用jstree ui时单击时打开它们的超链接

时间:2013-09-04 10:26:05

标签: javascript html jstree

我使用jtree显示层次结构,数据如下

<div id="songchanges"><ul>
<li id="phtml_1">
<a href="#">C:</a>
<ul>
<li id="phtml_2">
<a href="#">Music</a>
<ul>
<li id="phtml_3">
<a href="#">Z</a>
<ul>
<li id="phtml_4">
<a href="#">Hans Zimmer</a>
<ul>
<li id="phtml_5"><a href="FixSongsReport_00107_2013-09-04-11-11-55_body_blqxgT7E7YOmnBbjFXQGUg==.html">C:\Music\Z\Hans Zimmer\Hannibal</a></li>
</ul>
</li>
<li id="phtml_6">
<a href="#">The Zombies</a>
<ul>
<li id="phtml_7"><a href="FixSongsReport_00107_2013-09-04-11-11-55_body_er7mjWKbAaYaf8DygP84Fg==.html">C:\Music\Z\The Zombies\Best of The Zombies</a></li>
<li id="phtml_8"><a href="FixSongsReport_00107_2013-09-04-11-11-55_body_56XgVDhsjEKWXFd4OzVldA==.html">C:\Music\Z\The Zombies\The Zombies featuring Colin Blunstone & Rod Argent</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</div>

它显示没问题,作为文件系统。

这是框架集的一部分,显示在屏幕的左侧,当用户点击叶子节点时,我希望它打开右手边的附加链接(就像我将jtree东西应用到它之前一样) )

我的jtree配置如下

<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<link rel="stylesheet" type="text/css" href="songkong.css">
<base target="main">
<script type="text/javascript" src="http://static.jstree.com/v.1.0pre/jquery.js"></script>
<script type="text/javascript" src="http://static.jstree.com/v.1.0pre/jquery.cookie.js"></script>
<script type="text/javascript" src="http://static.jstree.com/v.1.0pre/jquery.hotkeys.js"></script>
<script type="text/javascript" src="http://static.jstree.com/v.1.0pre/jquery.jstree.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css" />
<script type="text/javascript" class="source below">
$(function () {
$("#songchanges")
.jstree({
"plugins" : ["themes","html_data","ui","crrm","hotkeys"],
"core" : { "initially_open" : [ "phtml_1" ] }
})
.bind("loaded.jstree", function (event, data) {
});
$("#songchanges").bind("open_node.jstree", function (e, data) {
data.inst.select_node("#phtml_1", true);
});
});
</script></head>

我阅读了demo3示例

$(function () {
    $("#demo3")
        .jstree({ "plugins" : ["themes","html_data","ui"] })
        // 1) if using the UI plugin bind to select_node
        .bind("select_node.jstree", function (event, data) { 
            // `data.rslt.obj` is the jquery extended node that was clicked
            alert(data.rslt.obj.attr("id"));
        })
        // 2) if not using the UI plugin - the Anchor tags work as expected
        //    so if the anchor has a HREF attirbute - the page will be changed
        //    you can actually prevent the default, etc (normal jquery usage)
        .delegate("a", "click", function (event, data) { event.preventDefault(); })
});

并尝试了但是.delegate选项没有效果,我假设我必须删除“ui”插件但尝试我的页面看起来就像我从未应用过jstree。

绑定选项有效,因为它会显示一个恼人的警报窗口,每次点击任何节点时都会显示id。那么如何更改它以在右手边打开链接呢?

更新 找到了这个答案Jstree nodes don't work when ui plugin is used

添加

  .bind("select_node.jstree", function (e, data) {
    var href = data.rslt.obj.children("a").attr("href");
    // this will load content into a div:
    $("#contents").load(href);
    // this will follow the link:
    document.location.href = href;
  }) 

现在链接在点击时打开,不幸的是它用页面替换了lefthandside框架,而我希望它将它放到右侧窗格中,任何想法。

5 个答案:

答案 0 :(得分:20)

用于新版本;

$("#demo2").jstree().bind("select_node.jstree", function (e, data) {
     var href = data.node.a_attr.href;
     document.location.href = href;
});

答案 1 :(得分:3)

我知道这篇文章有点老了,但是我在这里做了什么工作:

html:

char pickCard(){
    char aCard;
    std::cout << "Would you like another card?";
    std::cin >> aCard;
    if (aCard is a char){
        return aChar;
    } else {
        std::cout << "Not a char!";
    }
}

js:

<div id="entryPointTree">
    <ul>
        <li>Produits des mains
           <ul>
              <li><a href="http://www.google.ca" class="jstree-clicked">Lien 1</a></li>
              <li>item 2</li>
           </ul>
        </li>
        <li>Produits des pieds</li>
    </ul>
</div>

答案 2 :(得分:2)

试试这个。

$("#demo2").jstree().bind("select_node.jstree", function (e, data) { 
        var href = data.rslt.obj.children("a").attr("href"); 
        document.location.href = href; 

      //  $("#the_div").load(href); 
    }) ;

答案 3 :(得分:2)

我对v3.3.4的解决方案,它工作正常!

tree.jstree().on("click", ".jstree-anchor", function() {
    document.location.href = this.href;
});

答案 4 :(得分:0)

其他答案都没有做我想要的:节点标题中的多个while(true){ ActivityManager am = (ActivityManager) BackGroundService.this.getSystemService(ACTIVITY_SERVICE); List<ActivityManager.RunningAppProcessInfo> l = am.getRunningAppProcesses(); Iterator<ActivityManager.RunningAppProcessInfo> i = l.iterator(); while (i.hasNext()) { info = i.next(); if (info.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) { notificar("ola"); } try { sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } 标签。这对我有用:

<强> HTML

<a>

<强>的Javascript

<div id="object-tree">
  <ul>
     <li>SomeTitle <a href="/link1" target="_blank">link1><a href="/link2" target="_blank">link2</a></li>
  </ul>
</div>