2011-12-04 51 views
12

我是新來的Jquery和JS樹,但學習愛它。我使用php生成的xml設置了一個樹形菜單 (參見下面的代碼)。它的工作原理爲 ,但有一個例外 - 鏈接無效。JS樹鏈接不活躍

我知道有一些基本的東西我不明白。短期我只是 希望鏈接功能正常的鏈接。長期來看,我希望他們 觸發ajax調用,將重新加載頁面上的特定div。

任何人都可以指向正確的方向嗎?非常感謝您的幫助!

$(function() { 
     $("#mainMenu").jstree({ 
       xml_data : { data : <?php $menu->deliver(); ?> }, 
       core : { animation : 1000 } 
       ui : { select_limit : 1, selected_parent_close : false }, 
       themes : { theme : "default", dots : true, icons : false }, 
       types : { types : { "heading" : { select_node : true } } }, 
       plugins : [ "themes", "xml_data", "ui", "types" ] 
     }); 
}); 

示例XML(單項):

"<root><item id='pubPages_home' parent_id='0'><content><name href='? 
a=pubPages&amp;f=home'>Public Home</name></content></item><root>" 
+0

什麼鏈接不起作用?如果你點擊節點名稱?你想要發生什麼?你能提供jsfiddle樣本嗎? – Radek

+1

@Radek節點按預期工作(打開和關閉樹)。 a標籤hrefs不起作用。當懸停在鏈接上時,瀏覽器可以識別它們,但點擊不會將瀏覽器發送到鏈接。我懷疑JS樹已經調用preventDefault()來點擊一個標籤。 –

回答

2

我認爲你需要明確寫入了樹節點的鏈接點擊處理程序。 JsTree爲自己帶來點擊事件,不要讓它進入重定向頁面。

我會嘗試走這條路:

$('#mainMenu').bind('select_node.jstree', function(e,data) { 
    window.location.href = data.rslt.obj.attr("href"); 
}); 
3

我知道這是老了,但我來到這裏尋找一個快速解決。 LuborBílek大部分都是正確的,但請記住,jsTree將綁定單擊的錨元素的父項<li>

我解決了這個做:

.bind('select_node.jstree', function(e,data) { 
    window.location.href = data.rslt.obj.find('a').attr("href"); 
}); 

這樣一來,該事件將綁定到孩子<li>而不是<li>本身<a>,這樣你可以對你的錨href屬性,而不是您的列表項。

0

我找到一個簡單的方法。

$('#'+target).jstree({ 'core' : { 
    'data' : treeInfo 
} }); 

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

我發現jstree對li列表使用了「jstree-anchor」類。 所以,我可以從這個對象中找到href鏈接,我可以使這個鏈接沒有任何包圍功能。

1

我試試這個代碼:

window.location.href = data.rslt.obj.find('a').attr("href"); 

它提高data.rslt未定義的錯誤,所以我不能用它獲得的href!

這裏是我使用的碼:

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

是與jstree的版本?我jstree版本:3.0.0-bata10

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

jstree版本:「3.0。0" , 的jQuery:最後

更新: 或對我來說最好的辦法:

.bind("select_node.jstree", function (e, data) { 
    $('#jstree').jstree('save_state'); 
}) ; 

.on("activate_node.jstree", function(e,data){ 
    window.location.href = data.node.a_attr.href; 
}) 
1

我解決了,現在這個問題有點蠻力

<li id="child_node_1"><span onClick="javascript:window.location.href='your_page.html'" title="Lassie come home!">your text here</span></li> 

好,易於。

1

試試這個:

jQuery("#mytree ul").on("click","li.jstree-node a",function(){ 
    document.location.href = this; 
}); 

感謝jQuery event delegation,此事件將委託給<a>要素。 如果你不使用事件委託,它會在第一次點擊每個錨點時觸發。

+0

爲什麼OP要「試試這個」?你能提供一個解釋,這將有利於OP以及將來訪問SO嗎? –

1

通常我們需要一起控制「Container Nodes」和「Child Nodes」的動作。

因此,我們可以做thomething,如:

$("#jstree-div").jstree().delegate("a","click", function(e) { 
      if ($("#jstree-div").jstree("is_leaf", this)) { 
       document.location.href = this; 
      } 
      else { 
       $("#jstree-div").jstree("toggle_node", this); 
      } 
     }); 

「is_leaf」是來檢查,如果一個節點沒有孩子jsTree功能之一。