2011-02-02 60 views
7

我發現使用ui插件打破了樹節點的鏈接。這不是什麼新東西,我發現在其他地方引用了這個問題。第一個原因是jQuery驗證插件v1.6的問題。我沒有使用該插件,因此不能成爲原因。使用ui插件時Jstree節點不工作

我還發現了一篇很好的貼子,介紹了將jstree點擊的類添加到<a>標籤的幾種方法。這看起來很有希望,但是當我嘗試它時,我沒有注意到任何區別。這是一個很簡單的例子:

<div id="treediv"> 
    <ul> 
     <li id="page1"><a href="http://www.yahoo.com" class="jstree-clicked">YAHOO!</a></li>   
    </ul> 
</div> 
<script type="text/javascript" class="source"> 
$(function() { 

$("#treediv") 
    .jstree({ 
     "core" : { 
      "animation" : 0 
     }, 
     "themes" : { 
      "theme" : "classic" 
     }, 
     "plugins" : [ "themes", "html_data", "cookies", "ui" ] 
    }); 
}); 
</script> 

如果我拿出的UI插件,然後點擊鏈接帶我去yahoo.com預期。有沒有人有任何想法?

回答

10

我想我found the answer on the jstree discussion group。我相信UI插件允許節點「被選中」,但點擊不會傳遞到錨標籤。所以,我必須將一個函數綁定到一個節點被選中時執行。我做到了這一點與類似下面的.bind:

.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; 
    }) 

作爲一個附帶的好處,這個例子也顯示我是多麼容易點擊樹節點上,並顯示在另一個DIV動態內容。例如,假設樹節點被定義如下(採用html_data jstree插件和struts2的):

<li id="node1"> 
    <a href="do-something.action">Do Something</a> 
</li> 

點擊該樹的節點上會引起將要執行的DO-東西動作,其結果將顯示在與「內容」ID的div。

+1

+1!謝謝傑夫,這很好。 – 2011-09-11 20:18:11